emmett-framework / granian

A Rust HTTP server for Python applications
BSD 3-Clause "New" or "Revised" License
2.47k stars 75 forks source link

Thread panic in FastAPI streaming response #349

Closed bachya closed 2 weeks ago

bachya commented 2 weeks ago

I have a FastAPI app that returns a streaming response. I instantiate the app via this CLI command:

granian --interface asgi --host 0.0.0.0 --port 8000 --reload --log-level debug --access-log factory:APP

Irregularly (i.e., once every 2-3 tries), I get the following stacktrace:

thread '<unnamed>' panicked at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ptr/mod.rs:514:1:
Cannot drop pointer into Python heap without the GIL being held.
stack backtrace:
[23:29:31] info: [2024-07-08 23:29:29 +0000] 192.168.65.1 - "POST /api/v1/stream/ HTTP/1.1" 200 1407.781
   0:     0xffff94b3dee8 - <unknown>
   1:     0xffff94a7e8c0 - <unknown>
   2:     0xffff94b1bb50 - <unknown>
   3:     0xffff94b3f0ec - <unknown>
   4:     0xffff94b3ea44 - <unknown>
   5:     0xffff94b3fa04 - <unknown>
   6:     0xffff94b3f3ec - <unknown>
   7:     0xffff94b3f384 - <unknown>
   8:     0xffff94b3f378 - <unknown>
   9:     0xffff945e5730 - <unknown>
  10:     0xffff948b4404 - <unknown>
  11:     0xffff94788cd8 - <unknown>
  12:     0xffff946292ac - <unknown>
  13:     0xffff9464eae8 - <unknown>
  14:     0xffff94b4058c - <unknown>
  15:     0xffff9520ee58 - <unknown>
  16:     0xffff95277f9c - <unknown>
  17:                0x0 - <unknown>
[ERROR] Unexpected exit from worker-1
[INFO] Shutting down granian

Unfortunately, that's all. Happy to provide more info with some guidance.

gi0baro commented 2 weeks ago

This seems to be related to future cancellation. Gonna open up a PR for this later today. It would be quite helpful if you'll be able to test that branch directly building Granian from source.

bachya commented 2 weeks ago

I didn't get a chance to review before this was merged, but I was able to test the master branch:

granian[reload] @ git+https://github.com/emmett-framework/granian@master

Unfortunately, the issue persists—after a single HTTP request (with a streaming response), I see the same message:

thread '<unnamed>' panicked at /rustc/129f3b9964af4d4a709d1383930ade12dfe7c081/library/core/src/ptr/mod.rs:514:1:
Cannot drop pointer into Python heap without the GIL being held.
stack backtrace:
   0:     0xffffa7be26fc - <unknown>
   1:     0xffffa7b22b9c - <unknown>
   2:     0xffffa7bc0364 - <unknown>
   3:     0xffffa7be3900 - <unknown>
   4:     0xffffa7be3258 - <unknown>
   5:     0xffffa7be4218 - <unknown>
   6:     0xffffa7be3c00 - <unknown>
   7:     0xffffa7be3b98 - <unknown>
   8:     0xffffa7be3b8c - <unknown>
   9:     0xffffa7684c70 - <unknown>
  10:     0xffffa795a180 - <unknown>
  11:     0xffffa783a7cc - <unknown>
  12:     0xffffa76c54a8 - <unknown>
  13:     0xffffa76ee5c4 - <unknown>
  14:     0xffffa7be4da0 - <unknown>
  15:     0xffffa843ee58 - <unknown>
  16:     0xffffa84a7f9c - <unknown>
  17:                0x0 - <unknown>
[ERROR] Unexpected exit from worker-1
[INFO] Shutting down granian
gi0baro commented 2 weeks ago

@bachya can you provide a MRE so I can test it locally with the debug build?

gi0baro commented 2 weeks ago

@bachya I was able to reproduce this locally and just pushed 8332f65. If you can confirm this is fixed I'll close the issue and release a patch version.

bachya commented 2 weeks ago

@gi0baro Looks like https://github.com/emmett-framework/granian/commit/8332f65ccf1add5713a7509ae6c9f8ef6a695582 worked: I ran my streaming response request 10 times in a row and didn't see the previous crash. Thank you!

gi0baro commented 2 weeks ago

@bachya thank you for your patience! 1.5.1 release on the way ;)