Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hanging upload of large files #3489

Merged
merged 6 commits into from
Sep 11, 2024
Merged

Fix hanging upload of large files #3489

merged 6 commits into from
Sep 11, 2024

Conversation

noamblitz
Copy link
Contributor

Changes

Make bytes method async. Fixes hanging bytes client.

Issue link

Closes: #3488

Demo

Please add some proof in the form of screenshots or screen recordings to show (off) new functionality, if there are interesting new features for end-users.

QA notes

Please add some information for QA on how to test the newly created code.


Code Checklist

  • All the commits in this PR are properly PGP-signed and verified.
  • This PR only contains functionality relevant to the issue.
  • I have written unit tests for the changes or fixes I made.
  • I have checked the documentation and made changes where necessary.
  • I have performed a self-review of my code and refactored it to the best of my abilities.
  • Tickets have been created for newly discovered issues.
  • For any non-trivial functionality, I have added integration and/or end-to-end tests.
  • I have informed others of any required .env changes files if required and changed the .env-dist accordingly.
  • I have included comments in the code to elaborate on what is not self-evident from the code itself, including references to issues and discussions online, or implicit behavior of an interface.

Checklist for code reviewers:

Copy-paste the checklist from the docs/source/templates folder into your comment.


Checklist for QA:

Copy-paste the checklist from the docs/source/templates folder into your comment.

@noamblitz noamblitz requested a review from a team as a code owner September 9, 2024 13:09
Copy link
Contributor

@dekkers dekkers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The meta_repository.save_raw and event_manager.publish are sync methods. Calling sync methods from an async method will block the event loop. We could wrap those in sync_to_async, but I am still puzzled why async_to_sync doesn't seem to work correctly.

@noamblitz
Copy link
Contributor Author

Me too. Super weird. Wanted to put this out here so you could test it to see the problem.

@dekkers
Copy link
Contributor

dekkers commented Sep 9, 2024

Took me some time to figure out...

If you turn on asyncio debug mode you do get an exception:

[2024-09-09 18:19:13 +0000] [9] [ERROR] [base_events] Fatal error: protocol.data_received() call failed.
handle_traceback: Handle created at (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 1928, in _run_once
    handle._run()
  File "/usr/local/lib/python3.11/asyncio/events.py", line 84, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.11/site-packages/asgiref/sync.py", line 331, in main_wrap
    result = await self.awaitable(*args, **kwargs)
  File "/usr/local/lib/python3.11/site-packages/starlette/requests.py", line 244, in body
    async for chunk in self.stream():
  File "/usr/local/lib/python3.11/site-packages/starlette/requests.py", line 229, in stream
    message = await self._receive()
  File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 534, in receive
    self.flow.resume_reading()
  File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/flow_control.py", line 35, in resume_reading
    self._transport.resume_reading()
  File "/usr/local/lib/python3.11/asyncio/selector_events.py", line 852, in resume_reading
    self._add_reader(self._sock_fd, self._read_ready)
  File "/usr/local/lib/python3.11/asyncio/selector_events.py", line 917, in _add_reader
    self._loop._add_reader(fd, callback, *args)
  File "/usr/local/lib/python3.11/asyncio/selector_events.py", line 267, in _add_reader
    handle = events.Handle(callback, args, self, None)
protocol: <uvicorn.protocols.http.h11_impl.H11Protocol object at 0x7f0f7531e910>
transport: <_SelectorSocketTransport fd=11 read=idle write=<idle, bufsize=0>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/asyncio/selector_events.py", line 1013, in _read_ready__data_received
    self._protocol.data_received(data)
  File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 173, in data_received
    self.handle_events()
  File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 266, in handle_events
    self.cycle.message_event.set()
  File "/usr/local/lib/python3.11/asyncio/locks.py", line 192, in set
    fut.set_result(True)
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 764, in call_soon
    self._check_thread()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 801, in _check_thread
    raise RuntimeError(
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one

The problem is that async_to_sync seems to create its own event loop and that goes wrong with some code in starlette. So it seems we have to create the function the other way away, create it as an async and run the sync code in a safe way. It seems that the general way to do that with FastAPI is to call starlette.concurrency.run_in_threadpool so I did that here.

I also added an integration test that uploads a big file. This one failed before and succeeds now.

ammar92
ammar92 previously approved these changes Sep 10, 2024
Copy link
Contributor

@ammar92 ammar92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work

@noamblitz
Copy link
Contributor Author

Nice @dekkers, thanks for the additions. I still don't really understand whats going on here but glad it works now :).

@dekkers dekkers changed the title make method async Fix hanging upload of large files Sep 10, 2024
@stephanie0x00
Copy link
Contributor

Checklist for QA:

  • I have checked out this branch, and successfully ran a fresh make reset.
  • I confirmed that there are no unintended functional regressions in this branch:
    • I have managed to pass the onboarding flow
    • Objects and Findings are created properly
    • Tasks are created and completed properly
  • I confirmed that the PR's advertised feature or hotfix works as intended.
  • I checked the logs for errors and/or warnings and made issues where necessary

What works:

Seems to fix the async errors that were shown in the logs 👍

What doesn't work:

n/a

Bug or feature?:

n/a

@dekkers
Copy link
Contributor

dekkers commented Sep 11, 2024

The new create_raw doesn't use request.body anymore so doesn't have the problem. I think it would still be good to add the integration test and we need to backport the fix to 1.17.

@underdarknl underdarknl merged commit 68eef2c into main Sep 11, 2024
10 checks passed
@underdarknl underdarknl deleted the fix/hanging-bytes branch September 11, 2024 09:47
jpbruinsslot added a commit that referenced this pull request Sep 11, 2024
* origin:
  Fix hanging upload of large files (#3489)
  feat: multi select dropdown (#3446)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bytes async_to_sync method hangs
5 participants