8000 Correct threading usage in vendored AWB code by edan-bainglass · Pull Request #249 · aiidalab/aiidalab-home · GitHub
[go: up one dir, main page]

Skip to content

Correct threading usage in vendored AWB code - #249

Merged
edan-bainglass merged 2 commits into
mainfrom
fix-threads
Sep 28, 2026
Merged

edan-bainglass merged 2 commits into
mainfrom
fix-threads

Conversation

@edan-bainglass
@edan-bainglass edan-bainglass commented Sep 26, 2026 •
Copy link
Copy Markdown
Member

This PR modifies two threading issues that can hang a kernel marked for shutdown.

The old loop refreshed immediately, then slept for 30 seconds. It had no way to stop:

while True:
    self.update()
    time.sleep(update_interval)

The new loop waits on a threading.Event:

while not self._autoupdate_stop.wait(update_interval):
    self.update()

wait() returns False when the interval expires, so the table refreshes and the loop waits again. When stop_autoupdate() sets the event, wait() returns True immediately and the loop exits - no further waiting.

The first refresh now occurs after one interval, not immediately. That’s okay here because the widget already calls update() during construction. Closing a tab does not call stop_autoupdate() automatically. The daemon flag separately ensures this thread cannot hold up kernel shutdown.

Partially addresses aiidalab/aiidalab#538

@danielhollas
Copy link
Copy Markdown
Contributor

@edan-bainglass to clarify, what triggered this PR? Do you think this will solve the issue you saw with the kernels not being shutdown? If so, could you open an issue and describe how it can be deterministically re-produced?

The old loop refreshed immediately, then slept for 30 seconds. It had no way to stop:

I am not fully following the changes here, will need to take a closer look. But I would expect that if we simply mark the thread as daemonic (as you're doing in this PR) it should not block the kernel shutdown?

@edan-bainglass
edan-bainglass commented Sep 26, 2026 •
Copy link
Copy Markdown
Member Author

@edan-bainglass to clarify, what triggered this PR? Do you think this will solve the issue you saw with the kernels not being shutdown? If so, could you open an issue and describe how it can be deterministically re-produced?

The old loop refreshed immediately, then slept for 30 seconds. It had no way to stop:

I am not fully following the changes here, will need to take a closer look. But I would expect that if we simply mark the thread as daemonic (as you're doing in this PR) it should not block the kernel shutdown?

I've opened an issue here - aiidalab/aiidalab#538. I'll add an MRE there soon.
Also, much more on thread-safety in the new guideline - aiidalab/aiidalab#536

But yes, the suggestion, and implementation here, is to make these threads 🔥daemonic🔥

Note that regarding what was discussed the past few days, the primary offender was appmode (as suspected by @yakutovicha 😇). See issue there. This change alone takes care of much of the recently experienced issues. The remaining fixes (this PR, and similar ones on AWB and the QE app) aim to cover our backs w.r.t "bad" thread usage.

Comment thread home/process.py
self._monitor_thread_stop.clear()
self._monitor_thread = threading.Thread(
target=self._monitor_process, args=(process_uuid,)
target=self._monitor_process, args=(process_uuid,), daemon=True
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Making the thread daemonic so that it does not hold up a kernel marked for termination

@edan-bainglass
Copy link
Copy Markdown
Member Author

@cpignedoli please review 🙏

@cpignedoli cpignedoli left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, Edan! I reviewed 19473d4 with Codex assistance. The change looks good for the stated scope. Independent probes confirmed actual polling, prompt stop while waiting, restart, and normal Python exit with each daemon worker; the exact base remained waiting on those workers.

One small, non-blocking test suggestion: call refreshed.clear() immediately after constructing ProcessListWidget and before start_autoupdate(). Construction already calls update(), so the current refreshed.wait() also passes when the background worker never refreshes. I confirmed this with a worker that only waits for the stop event.

The two new tests passed locally with --noconftest (the global PostgreSQL fixture setup was unavailable), along with 12 additional probes. CI is green, including the Chrome and Firefox notebook checks. The documented scope around daemon shutdown and explicit polling cleanup is appropriate. Looks good to me!

@edan-bainglass
edan-bainglass merged commit c3b2004 into main Sep 28, 2026
5 checks passed
@edan-bainglass
edan-bainglass deleted the fix-threads branch September 28, 2026 05:02
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.

3 participants

0