Repository navigation
fix: make Session.schedule_update() and schedule_effect() thread-safe - #7113
joaoaracheski wants to merge 6 commits into
Conversation
Both changed the pending queues and called asyncio.Event.set() on the calling thread. From a worker thread (e.g. state changed in a page.run_thread() handler) the set() doesn't wake an idle loop, so the update waited for something else to wake it, and in asyncio debug mode it raised and left the scheduler stuck for the rest of the session. Calls from other threads are now handed to the scheduler's loop with call_soon_threadsafe(), so the queues are only touched on the loop and no lock is needed. Fixes flet-dev#7108
|
Hey @joaoaracheski — I had some free time and came across your PR here, so I ran it against the scenarios I mentioned in #7108. Thought you'd want the results. I set up an isolated env with stock flet 1.1.2 and ran your test file both before and after the patch:
So While I was in there, I spotted a few edge paths that could use a bit of hardening:
None of these are blockers — the core fix is solid. Since I was already in there, I'd be glad to put together a small patch with regression tests for these three and tack it onto your branch if you'd like — just say the word. Or if you'd rather handle them yourself, the descriptions are all above. Totally your call. Thanks again for picking this up! |
…sconnects Co-authored-by: daneeller <283215001+daneeller@users.noreply.github.com>
|
@daneeller good catches, and the run against stock 1.1.2 is a nice confirmation. 1 and 2 are fixed now. I left 3 out on purpose. Before |
…keups - set_state called off the event loop posts the change to the loop, in the render's context, so it can't race a render of the component. - Worker threads post at most one scheduler wakeup at a time, outside the queue lock. - Queuing work on the loop starts the scheduler, so apps that don't render components don't keep queued updates forever. - One __accepting_work() check for queuing and for effects waiting on async cleanups; the queue lock now also covers conn on disconnect. - Use the connection's loop instead of a separate __updates_loop. - close() and disconnect() drop the queued work after releasing the lock, so a finalizer that queues work can't deadlock. - Tests: wait for the close task in teardown; cover coalesced wakeups, scheduler start, finalizers and set_state from worker threads. - Changelog entry from the app developer's perspective.
|
Thanks for this! I reviewed the PR and pushed a commit on top (a6f7dff) that addresses what I found. Summary below. Review findings and fixes
One thing I left as is: mutating an PerformanceBest of several runs, Python 3.14, macOS arm64:
|
…hread-safe # Conflicts: # CHANGELOG.md # sdk/python/packages/flet/src/flet/messaging/session.py
Description
Fixes #7108.
Session.schedule_update()andschedule_effect()changed the pending queues and calledasyncio.Event.set()on the calling thread. In a components app that's reachable from a worker thread: state changed in apage.run_thread()handler notifies the component, which callsschedule_update()there.What happens today when that runs on a worker thread:
Event.set()doesn't wake an idle loop, so the update sits there until something else wakes it (a message from the client, a timer). In my test the update didn't arrive in 5 s.RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one, the update is lost and the scheduler task stays stuck, so no later update of that session goes out.asyncio.run()even hangs on shutdown.The fix hands calls from other threads to the scheduler's loop with
call_soon_threadsafe(). The queues and theEventare only touched on the loop, so there's no race betweenaddand the scheduler'sclear(), and no lock is needed. On the loop thread nothing changes: the control is queued right away. If the loop is already closed (a worker that outlives the session), the call is dropped instead of raising.schedule_effect()had the same problem, so it goes through the same path.Test code
Before:
not applied in 5 sAfter:
applied(right away)New
tests/test_schedule_update_threads.py:flet: 565 passed, 8 skipped. ruff check and format clean.
Type of change
Checklist
website/sidebars.ymlfor breaking changes, removals, and deprecations, if applicable.Additional details
This covers the scheduler on
main. It doesn't makepage.update()itself thread-safe: that one still runspatch_control()on the calling thread, which is #7107. Kept it out to keep this PR small.Before the scheduler starts (no components mode yet), calls still run directly like before, since nothing is draining the queues at that point.
Summary by Sourcery
Make session scheduling and component state updates safe and reliable when invoked from worker threads.
Bug Fixes:
Enhancements:
Tests: