Skip to content

Keep GUI responsive during ICA runs - #190

Merged
arnodelorme merged 15 commits into
developfrom
fix/async-ica-progress
Jun 12, 2026
Merged

arnodelorme merged 15 commits into
developfrom
fix/async-ica-progress

Conversation

@suraj-ranganath

Copy link
Copy Markdown
Member

Summary

This is a stacked PR targeting origin/develop. It is built on top of the existing open stack, especially #177, and should not be merged before the earlier stacked PRs land.

Implemented a reusable Qt long-task path and wired GUI-launched ICA through it so long pop_runica runs no longer freeze the main EEGPrep window.

What changed

  • Added eegprep.functions.guifunc.long_task.run_long_task, a Qt worker-thread helper with an indeterminate progress dialog and EEGPrep log forwarding.
  • Split pop_runica GUI option collection from computation via pop_runica_gui_options(...), so the options dialog stays on the UI thread and only ICA computation runs in the worker.
  • Routed GUI pop_runica through the long-task helper when launched from a Qt parent.
  • Kept all EEGPrepSession mutation on the main thread after worker success. Failed ICA runs leave EEG/session/history unchanged.
  • Preserved mixed GUI + eegprep-console command/progress ordering by keeping the GUI-action boundary open until the background task finishes.
  • Updated pop_runica help, GUI/console workflow docs, and implementation notes.

Verification

  • uv run --no-sync pytest tests/test_gui_long_task.py tests/test_gui_main_window.py::MenuActionDispatcherTests::test_gui_pop_runica_runs_ica_in_long_task_before_committing_result tests/test_gui_main_window.py::MenuActionDispatcherTests::test_gui_pop_runica_long_task_error_does_not_mutate_session tests/test_gui_pop_runica.py
  • uv run --no-sync pytest tests/test_gui_main_window.py tests/test_console_workspace.py::test_gui_action_without_command_releases_output_through_terminal_redraw tests/test_console_workspace.py::test_gui_pop_action_warning_output_follows_echoed_command
  • EEGPREP_SKIP_MATLAB=1 uv run --no-sync pytest -m "not slow" -> 1957 passed, 215 skipped, 12 deselected
  • uv run --no-sync ruff check .
  • uv run --no-sync ruff format --check .
  • uv run --no-sync ty check
  • ./pre-commit.py --changed-from origin/develop

Notes

The progress dialog is intentionally indeterminate because runica does not expose a reliable percentage. I did not add a Cancel button because cancellation would be fake until the ICA backends support safe interruption.

@claude

claude Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @suraj-ranganath's task in 4m 38s —— View job


Code review

This is a stacked PR; I scoped the review to the change this PR actually introduces (commit 48fad4a: long_task.py, the pop_runica GUI/compute split, and the menu_actions wiring). The remaining ~95 files are inherited from earlier PRs in the stack (#177 etc.) and are out of scope here.

  • Overall assessment: needs changes (one threading concern to verify before merge)
  • Highest-risk area: Qt signal/slot thread affinity in run_long_task
  • Merge recommendation: needs changes

The split of pop_runica into pop_runica_gui_options(...) (UI thread) + computation (worker) is clean, the **gui_options round-trip through pop_runica(..., gui=False) maps correctly onto the explicit params and **kwargs (dataset/concatenate/concatcond are parsed and unused on the single-dataset path), and the begin/end GUI-action boundary is balanced (the except in _run_pop_runica_long_task calls end_gui_action, and on_finished calls it on completion). Error handling correctly leaves the session unmutated.

Blocking

None confirmed (see Important — I could not run a live multi-threaded repro in this sandbox).

Important

Result/progress callbacks almost certainly run on the worker thread, not the UI thread — src/eegprep/functions/guifunc/long_task.py

The PR's central claim is "all EEGPrepSession mutation on the main thread after worker success." But the worker is moveToThread(thread) and emits succeeded/failed/message from run() on the worker thread, while the handlers are connected as context-free Python callables:

worker.message.connect(lambda message: _update_progress_label(progress, label, message))
worker.succeeded.connect(on_success)
worker.failed.connect(on_error)

In Qt, an AutoConnection to a functor with no receiver QObject resolves to a DirectConnection, so the slot executes in the emitting (worker) thread. That means:

  • on_success → _store_current_from_gui() + _refresh() mutate the session and redraw the GUI off the main thread.
  • _update_progress_label() mutates a QProgressDialog (a QWidget) off the main thread during the run.

Both are exactly the off-main-thread GUI access this PR set out to avoid, and they tend to fail intermittently (sporadic segfaults) rather than in tests. Nothing in the helper marshals back to the GUI thread (no context object, no QMetaObject.invokeMethod, no main-thread receiver).

Suggested fix: anchor the result-handling connections to a main-thread QObject so delivery is queued onto the GUI thread — e.g. pass a context receiver to connect (worker.succeeded.connect(context_obj, on_success)) or route through a small QObject whose real slots live on the main thread. The worker.finished -> thread.quit and thread.finished -> progress.close connections are already fine because those receivers (thread, progress) live on the main thread.

Please verify empirically before merge, e.g. assert threading.get_ident() inside on_success equals the main thread id. Fix this →

Nits

None.

Test gaps

  • No test asserts the thread the callbacks run on, which is the property the PR is built around. tests/test_gui_main_window.py mocks run_long_task entirely (so the menu path never touches a real thread), and tests/test_gui_long_task.py only checks return values/labels, not thread affinity. Add a test_gui_long_task.py case that records threading.get_ident() inside on_success/on_error/the progress-label update and asserts it equals the main thread id. This both documents the contract and would catch the issue above.

EEGLAB parity notes

None. Keeping console-invoked ICA synchronous (parent is None) while routing GUI-invoked ICA through an indeterminate progress dialog is consistent with EEGLAB UX, and the decision to omit a Cancel button (no safe interruption in the backends) is reasonable.
• fix/async-ica-progress

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