Skip to content

Commit b31df28

Browse files
zandersocommit-bot@chromium.org
authored andcommitted
[dart:io] Retain the stack trace on some native socket errors
Currently, if a the write() system call fails on a socket, the stack trace in Dart code of the synchronous call to write is dropped. This CL retains the stack trace. Change-Id: I926ca98073d795acc2eb9656dc1c6371ac5b009c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112650 Reviewed-by: Siva Annamalai <asiva@google.com> Commit-Queue: Zach Anderson <zra@google.com>
1 parent 22866b6 commit b31df28

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

runtime/bin/socket_patch.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
528528
s.setListening(read: false, write: false);
529529
});
530530
connecting.clear();
531-
}, error: (e) {
531+
}, error: (e, st) {
532532
timer.cancel();
533533
socket.close();
534534
// Keep first error, if present.
@@ -676,7 +676,7 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
676676
if (len == 0) return null;
677677
var result = nativeRead(len);
678678
if (result is OSError) {
679-
reportError(result, "Read failed");
679+
reportError(result, StackTrace.current, "Read failed");
680680
return null;
681681
}
682682
if (result != null) {
@@ -699,7 +699,7 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
699699
if (isClosing || isClosed) return null;
700700
var result = nativeRecvFrom();
701701
if (result is OSError) {
702-
reportError(result, "Receive failed");
702+
reportError(result, StackTrace.current, "Receive failed");
703703
return null;
704704
}
705705
if (result != null) {
@@ -747,7 +747,8 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
747747
nativeWrite(bufferAndStart.buffer, bufferAndStart.start, bytes);
748748
if (result is OSError) {
749749
OSError osError = result;
750-
scheduleMicrotask(() => reportError(osError, "Write failed"));
750+
StackTrace st = StackTrace.current;
751+
scheduleMicrotask(() => reportError(osError, st, "Write failed"));
751752
result = 0;
752753
}
753754
// The result may be negative, if we forced a short write for testing
@@ -776,7 +777,8 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
776777
bytes, (address as _InternetAddress)._in_addr, port);
777778
if (result is OSError) {
778779
OSError osError = result;
779-
scheduleMicrotask(() => reportError(osError, "Send failed"));
780+
StackTrace st = StackTrace.current;
781+
scheduleMicrotask(() => reportError(osError, st, "Send failed"));
780782
result = 0;
781783
}
782784
// TODO(ricow): Remove when we track internal and pipe uses.
@@ -934,7 +936,7 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
934936

935937
if (i == errorEvent) {
936938
if (!isClosing) {
937-
reportError(nativeGetError(), "");
939+
reportError(nativeGetError(), null, "");
938940
}
939941
} else if (!isClosed) {
940942
// If the connection is closed right after it's accepted, there's a
@@ -1092,11 +1094,11 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
10921094
}
10931095
}
10941096

1095-
void reportError(error, String message) {
1097+
void reportError(error, StackTrace st, String message) {
10961098
var e = createError(error, message, address, localPort);
10971099
// Invoke the error handler if any.
10981100
if (eventHandlers[errorEvent] != null) {
1099-
eventHandlers[errorEvent](e);
1101+
eventHandlers[errorEvent](e, st);
11001102
}
11011103
// For all errors we close the socket
11021104
close();
@@ -1242,8 +1244,8 @@ class _RawServerSocket extends Stream<RawSocket> implements RawServerSocket {
12421244
_controller.add(new _RawSocket(socket));
12431245
if (_controller.isPaused) return;
12441246
}
1245-
}), error: zone.bindUnaryCallbackGuarded((e) {
1246-
_controller.addError(e);
1247+
}), error: zone.bindBinaryCallbackGuarded((e, st) {
1248+
_controller.addError(e, st);
12471249
_controller.close();
12481250
}), destroyed: () {
12491251
_controller.close();
@@ -1346,8 +1348,8 @@ class _RawSocket extends Stream<RawSocketEvent> implements RawSocket {
13461348
_controller.add(RawSocketEvent.closed);
13471349
_controller.close();
13481350
},
1349-
error: zone.bindUnaryCallbackGuarded((e) {
1350-
_controller.addError(e);
1351+
error: zone.bindBinaryCallbackGuarded((e, st) {
1352+
_controller.addError(e, st);
13511353
_socket.close();
13521354
}));
13531355
}
@@ -1889,8 +1891,8 @@ class _RawDatagramSocket extends Stream<RawSocketEvent>
18891891
_controller.add(RawSocketEvent.closed);
18901892
_controller.close();
18911893
},
1892-
error: zone.bindUnaryCallbackGuarded((e) {
1893-
_controller.addError(e);
1894+
error: zone.bindBinaryCallbackGuarded((e, st) {
1895+
_controller.addError(e, st);
18941896
_socket.close();
18951897
}));
18961898
}

0 commit comments

Comments
 (0)