Skip to content

Commit 10d3900

Browse files
authored
Don't crash if user calls sys.exit() (#434)
**Issue:** If user called `sys.exit()` from a WebSocket callback, the program would exit non-gracefully, printing out a big old C stack trace. **Diagnosis:** The websocket callbacks are doing some [extra zealous error checking](https://github.com/awslabs/aws-crt-python/blob/edf0ef624bd84ab6d669b9b8639e2d9a43140c95/source/websocket.c#L246-L250) when calling from C->Python. The Python code is supposed to catch and handle any exceptions resulting from the user's code. C will murder the applications if any other exceptions leak back to C, with the thinking that these must result from bugs in OUR code, so best to just die. Anyway, `sys.exit()` results in a `SystemExit` exception, which inherits from`BaseException` instead of `Exception`, so it was slipping through the Python code's `except Exception:` filter. **Description of changes:** Handle `BaseException` (not just ~Exception~) in the Python section of the callback, so that `SystemExit` doesn't leak back into C and trigger the extra zealous error checker.
1 parent edf0ef6 commit 10d3900

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

awscrt/websocket.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def _on_complete(error_code):
388388
try:
389389
if on_complete is not None:
390390
on_complete(cbdata)
391-
except Exception:
391+
except BaseException:
392392
print("Exception in WebSocket.send_frame on_complete callback", file=sys.stderr)
393393
sys.excepthook(*sys.exc_info())
394394
self.close()
@@ -457,7 +457,7 @@ def _on_connection_setup(
457457
# Do not let exceptions from the user's callback bubble up any further.
458458
try:
459459
self._on_connection_setup_cb(cbdata)
460-
except Exception:
460+
except BaseException:
461461
print("Exception in WebSocket on_connection_setup callback", file=sys.stderr)
462462
sys.excepthook(*sys.exc_info())
463463
if cbdata.websocket is not None:
@@ -472,7 +472,7 @@ def _on_connection_shutdown(self, error_code):
472472
try:
473473
if self._on_connection_shutdown_cb is not None:
474474
self._on_connection_shutdown_cb(cbdata)
475-
except Exception:
475+
except BaseException:
476476
print("Exception in WebSocket on_connection_shutdown callback", file=sys.stderr)
477477
sys.excepthook(*sys.exc_info())
478478

@@ -485,7 +485,7 @@ def _on_incoming_frame_begin(self, opcode_int, payload_length, fin):
485485
try:
486486
if self._on_incoming_frame_begin_cb is not None:
487487
self._on_incoming_frame_begin_cb(cbdata)
488-
except Exception:
488+
except BaseException:
489489
print("Exception in WebSocket on_incoming_frame_begin callback", file=sys.stderr)
490490
sys.excepthook(*sys.exc_info())
491491
return False # close websocket
@@ -499,7 +499,7 @@ def _on_incoming_frame_payload(self, data):
499499
try:
500500
if self._on_incoming_frame_payload_cb is not None:
501501
self._on_incoming_frame_payload_cb(cbdata)
502-
except Exception:
502+
except BaseException:
503503
print("Exception in WebSocket on_incoming_frame_payload callback", file=sys.stderr)
504504
sys.excepthook(*sys.exc_info())
505505
return False # close websocket
@@ -517,7 +517,7 @@ def _on_incoming_frame_complete(self, error_code):
517517
try:
518518
if self._on_incoming_frame_complete_cb is not None:
519519
self._on_incoming_frame_complete_cb(cbdata)
520-
except Exception:
520+
except BaseException:
521521
print("Exception in WebSocket on_incoming_frame_complete callback", file=sys.stderr)
522522
sys.excepthook(*sys.exc_info())
523523
return False # close websocket

0 commit comments

Comments
 (0)