Skip to content

Don't miss queue changes announced while the worker polls queues - #101

Closed
luca-montaigut wants to merge 1 commit into
TkTech:26_uxfrom
luca-montaigut:fix-lost-queue-wakeup
Closed

luca-montaigut wants to merge 1 commit into
TkTech:26_uxfrom
luca-montaigut:fix-lost-queue-wakeup

Conversation

@luca-montaigut

Copy link
Copy Markdown
Contributor

The worker sometimes misses a queue.declared / queue.paused / queue.resumed notification and only sees the change at the next queue_change_poll_interval (10s by default). This is probably why test_regression_48_eager still fails intermittently on #58.

The problem

_maintain_queues loops:

  1. read the queues from the database;
  2. await self.hub.wait_for([...], timeout=queue_change_poll_interval);
  3. repeat.

Hub.wait_for() registers its waiters when it is called and drops them when it returns, and Hub.emit() only reaches the waiters registered at that moment. If the notification arrives during step 1, nothing is waiting for it and it is lost. The worker then sleeps the full 10s before polling again.

On #58's last run (ubuntu-24.04 - Py3.14 - Django5.2 - PG14), the eager queue is declared at 09:32:34 and the worker logs Adding queue 'eager' at 09:32:44: exactly one poll interval later, with the test's wait_for_jobs(..., timeout=10) already used up.

The fix

The same pattern the worker already uses for queue.pushed: there, _handle_queue_pushed sets queue_wake_events[q] and the queue loop waits on that event. Here:

  • a _handle_queues_changed hub handler, registered in start() for the three events, sets self._queues_changed;
  • _maintain_queues clears it right before reading the queues, then waits on it, with the same queue_change_poll_interval timeout, instead of on hub.wait_for().

A change announced during the poll leaves the event set, so the next wait returns immediately. The 10s poll is still there as a fallback for notifications that never arrive.

Testing

  • New test_queue_declared_during_poll_is_not_missed. It holds the worker right after it has read the queues, declares a queue, waits until the notification has reached the worker's hub, then checks the queue is picked up within 3s. It fails every time without the fix and passes in about 0.2s with it.
  • Full suite on Python 3.11: 133 passed, 13 skipped.

To be clear about what this proves: the new test shows the lost wakeup and that the fix closes it. I could not make test_regression_48_eager itself fail locally, before or after the fix. The link to the failure on #58 rests on the 10s gap in the CI log above.

🤖 Generated with Claude Code

_maintain_queues listened for queue.declared/paused/resumed only while
inside hub.wait_for(). An event emitted while it was reading the queues
from the database had no waiter and was dropped, so the change was only
seen at the next queue_change_poll_interval (10s by default).

Record these events in an asyncio.Event set by a hub handler, cleared
before each poll, the same way queue.pushed already wakes queues through
queue_wake_events.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@luca-montaigut
luca-montaigut marked this pull request as ready for review October 1, 2026 20:50
@codecov

codecov Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.52%. Comparing base (a53daaf) to head (ca000d6).

Additional details and impacted files
@@            Coverage Diff             @@
##            26_ux     #101      +/-   ##
==========================================
+ Coverage   67.44%   67.52%   +0.08%     
==========================================
  Files          62       62              
  Lines        3781     3788       +7     
==========================================
+ Hits         2550     2558       +8     
+ Misses       1231     1230       -1     
Flag Coverage Δ
unittests 67.52% <100.00%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@luca-montaigut
luca-montaigut marked this pull request as draft October 1, 2026 20:54
@luca-montaigut
luca-montaigut deleted the fix-lost-queue-wakeup branch October 1, 2026 21:10
@luca-montaigut

Copy link
Copy Markdown
Contributor Author

Closing this, sorry for the noise. I was investigating the intermittent test_regression_48_eager failure on #58 and opened this too early. The lost wakeup it fixes is real, but I couldn't show it is what makes test_48 fail, and CI then failed on a trigger test I couldn't reproduce either. Not worth your time without stronger evidence.

One observation, in case it helps: in the failing test_48 run on #58, the eager queue is declared at 09:32:34 but the worker only adds it at 09:32:44, one full queue_change_poll_interval, which uses up the test's 10s timeout.

@TkTech

TkTech commented Oct 2, 2026

Copy link
Copy Markdown
Owner

Thanks for taking a look either way! I've been reworking the fixtures which should outright make these issues impossible within the trivial tests. For the most part the tests are just too down-to-the-second with timeouts and the github workers can be incredibly slow.

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.

2 participants