Skip to content

Commit 4128bcf

Browse files
authored
Merge pull request #22 from atsign-foundation/fix-socket-closed-while-other-side-still-writing
fix: correctly handle situation where a socket has been closed but the other side is still writing to it
2 parents a0e4285 + 75b738b commit 4128bcf

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
## 2.3.1
2+
3+
- fix: correctly handle situation where a socket has been closed but the other
4+
side is still writing to it
5+
16
## 2.3.0
27

38
- feat: Add `authTimeout` property to SocketConnector. Previously this was
4-
- hard-coded to 5 seconds which is a bit restrictive for slow network
9+
hard-coded to 5 seconds which is a bit restrictive for slow network
510
connections. It now defaults to 30 seconds but may be set at time of
611
construction
712
- feat: added `authTimeout` parameter to `serverToServer`; this is provided

lib/src/socket_connector.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,17 @@ class SocketConnector {
141141
StreamController<Uint8List> sc = StreamController<Uint8List>();
142142
side.farSide!.sink = sc;
143143
Stream<List<int>> transformed = side.transformer!(sc.stream);
144-
transformed.listen(side.farSide!.socket.add);
144+
transformed.listen((event) {
145+
try {
146+
side.farSide!.socket.add(event);
147+
} catch (e) {
148+
_log('Failed to write to side ${side.farSide!.name} - closing',
149+
force: true);
150+
_destroySide(side.farSide!);
151+
}
152+
});
145153
}
146-
side.stream.listen((Uint8List data) async {
154+
side.stream.listen((Uint8List data) {
147155
if (logTraffic) {
148156
final message = String.fromCharCodes(data);
149157
if (side.isSideA) {
@@ -154,7 +162,13 @@ class SocketConnector {
154162
'B -> A : ${message.replaceAll(RegExp('[\x00-\x1F\x7F-\xFF]'), '*')}'));
155163
}
156164
}
157-
side.farSide!.sink.add(data);
165+
try {
166+
side.farSide!.sink.add(data);
167+
} catch (e) {
168+
_log('Failed to write to side ${side.farSide!.name} - closing',
169+
force: true);
170+
_destroySide(side.farSide!);
171+
}
158172
}, onDone: () {
159173
_log('stream.onDone on side ${side.name}');
160174
_destroySide(side);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: socket_connector
22
description: Package for joining sockets together to create socket relays.
33

4-
version: 2.3.0
4+
version: 2.3.1
55
repository: https://github.com/cconstab/socket_connector
66

77
environment:

0 commit comments

Comments
 (0)