cirospaciari / socketify.py

Bringing Http/Https and WebSockets High Performance servers for PyPy3 and Python3
https://www.socketify.dev
MIT License
1.44k stars 56 forks source link

Error when exiting Hello World program #40

Closed sean324 closed 1 year ago

sean324 commented 1 year ago

Describe the bug When I run the Hello World program in the README and then exit it with Control-C (on macOS) it gives the following error:

Listening on port http://localhost:3000 now

^CFatal Python error: b_from_handle: ffi.from_handle() detected that the address passed points to garbage. If it is really the result of ffi.new_handle(), then the Python object has already been garbage collected
Python runtime state: initialized

Current thread 0x0000000102794580 (most recent call first):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/socketify-env/lib/python3.10/site-packages/cffi/api.py", line 544 in from_handle
  File "/opt/homebrew/Caskroom/miniconda/base/envs/socketify-env/lib/python3.10/site-packages/socketify/uv.py", line 76 in socketify_generic_handler
  File "/opt/homebrew/Caskroom/miniconda/base/envs/socketify-env/lib/python3.10/site-packages/socketify/uv.py", line 152 in run
  File "/opt/homebrew/Caskroom/miniconda/base/envs/socketify-env/lib/python3.10/site-packages/socketify/loop.py", line 63 in run
  File "/opt/homebrew/Caskroom/miniconda/base/envs/socketify-env/lib/python3.10/site-packages/socketify/socketify.py", line 2513 in run
  File "/Users/sean/Documents/socketify-test/hello.py", line 6 in <module>

Extension modules: _cffi_backend (total: 1)
Abort trap: 6

To Reproduce Steps to reproduce the behavior:

  1. Create Miniconda environment with condo create -n socketify-test python=3.10 and then activate it.
  2. Install socketify with pip install git+https://github.com/cirospaciari/socketify.py.git
  3. Run the Hello World program that's in the README.
  4. Hit Control-C.
  5. See error.

Expected behavior To see no error.

Desktop (please complete the following information):

cirospaciari commented 1 year ago

This only occurs in CPython when exiting without gracefully shutdown https://github.com/cirospaciari/socketify.py/blob/main/examples/graceful_shutdown.py

This do not impact in the actual program but I will add an gracefully shutdown by default when exiting with signals, PyPy do not have the same behavior, CPython implementation of CFFI maybe be the issue

cirospaciari commented 1 year ago

@seanr3 fixed on commit https://github.com/cirospaciari/socketify.py/commit/75ee2771717d7c566b41609bd6234cee749c396a

sean324 commented 1 year ago

As part of the graceful shutdown by default, can you have it disconnect existing WebSockets and shutdown? Currently, if you hit Control-C (on macOS) it'll gracefully shutdown if there are no existing WebSockets, but if there are existing WebSockets then it won't shutdown until the last WebSocket is closed from the client's end.

cirospaciari commented 1 year ago

@seanr3 I think this is related to https://github.com/uNetworking/uWebSockets/discussions/1528

I will add docs about filter in python soon

cirospaciari commented 1 year ago

As part of the graceful shutdown by default, can you have it disconnect existing WebSockets and shutdown? Currently, if you hit Control-C (on macOS) it'll gracefully shutdown if there are no existing WebSockets, but if there are existing WebSockets then it won't shutdown until the last WebSocket is closed from the client's end.

@seanr3 close behavior with Control-C fixed in v0.0.3 see https://github.com/cirospaciari/socketify.py/releases/tag/v0.0.3