Repository navigation
Allow IProgressService.run() to be called from any thread - #4587
fedejeanne wants to merge 1 commit into
Conversation
Test Results 867 files ± 0 867 suites ±0 58m 19s ⏱️ - 1m 48s Results for commit ddbf1b0. ± Comparison against base commit 50268ca. This pull request removes 2 and adds 56 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
There was a problem hiding this comment.
🟡 Changes recommended
Different E3 and E4 worker markers cause nested cross-framework runs to fork again, leaving the runnable without ownership of the outer worker’s scheduling rules or locks.
1 open issue
What changed in this PR
This PR allows E3 and E4 progress services to accept run(...) calls from background threads, addressing the thread-affinity inconsistency discussed in #4202 while preserving existing UI-thread behavior.
Changes:
- Adds background execution without progress dialogs, including fork, interrupt, exception, and thread-listener handling.
- Documents the threading contract and updates the example tooltip.
- Adds E3/E4 regression tests, registers them in the progress suite, and updates bundle dependencies.
| File | Description |
|---|---|
tests/org.eclipse.ui.tests/META-INF/MANIFEST.MF |
Adds the E4 progress bundle dependency. |
tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/progress/ProgressTestSuite.java |
Registers UI-thread and background-thread service tests. |
tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/progress/ProgressServiceTest.java |
Refines UI-thread dialog detection and waiting. |
tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/progress/ProgressServiceOffUIThreadTest.java |
Applies shared background-thread tests to E3. |
tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/progress/E4ProgressServiceOffUIThreadTest.java |
Applies shared background-thread tests to E4. |
tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/progress/AbstractProgressServiceOffUIThreadTest.java |
Adds shared threading, exception, scheduling-rule, and deadlock regression tests. |
examples/org.eclipse.ui.examples.job/src/org/eclipse/ui/examples/jobs/views/ProgressServiceView.java |
Clarifies background-call behavior in the tooltip. |
bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/progress/IProgressService.java |
Documents E3 threading behavior and restrictions. |
bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/progress/ProgressManager.java |
Implements background-thread execution for E3. |
bundles/org.eclipse.e4.ui.progress/src/org/eclipse/e4/ui/progress/IProgressService.java |
Documents E4 threading behavior and restrictions. |
bundles/org.eclipse.e4.ui.progress/src/org/eclipse/e4/ui/progress/internal/ProgressServiceImpl.java |
Implements background-thread execution for E4. |
bundles/org.eclipse.e4.ui.progress/META-INF/MANIFEST.MF |
Bumps the bundle version to 0.6.0. |
Review effort: Max
badb645 to
1c0e22d
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Relaxing the public threading contract lets previously rejected off-UI calls execute and may cause scheduling-rule deadlocks, requiring maintainer judgment on third-party compatibility and release risks.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
The workbench and the E4 IProgressService are IRunnableContexts, whose run() has no thread restriction. Nevertheless, run() fails when it is called from a thread other than the UI thread, because it always creates a progress dialog: the workbench service throws an IllegalStateException, the E4 service an SWTException. Clients that receive an IRunnableContext and use it from a background thread, for example from a Job, cannot be given the progress service and have to work around this. run() now works from any thread. From the UI thread nothing changes. From another thread no progress dialog opens and the user cannot cancel. As IRunnableContext specifies, the runnable runs in the calling thread when fork is false and in a separate thread when fork is true. In both cases the calling thread blocks until the runnable has finished, and exceptions are reported as for any other IRunnableContext. The separate thread is a plain thread, not a ModalContext thread: a ModalContext thread ends with a syncExec, so a background caller would deadlock while the UI thread waits for it. As in ModalContext, a caller that already runs in a ModalContext thread or in a thread forked by a progress service does not fork again, so a nested runnable keeps the scheduling rules and locks of that thread. Both services share one internal JFace implementation, so this also holds when a runnable of one service calls the other. runInUI() already worked from any thread. The class javadoc now says so. The new off-UI-thread tests for both services are part of ProgressTestSuite, so that CI runs them. The UI-thread tests in ProgressServiceTest now wait for the progress dialog instead of relying on a fixed delay, so that they are stable in CI. Contributes to: eclipse-platform#4202 Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1c0e22d to
ddbf1b0
Compare

Contributes to: #4202
Problem
IProgressService(in E3 and in E4) extendsIRunnableContext. The contract ofIRunnableContext#runhas no thread restriction. It only says: "Ifforkisfalse, the current thread is used to run the runnable".IProgressServicenarrows this contract with "All methods on the progress service must be called from the UI thread". Its implementations fail when they are called from another thread:ProgressManagerthrows anIllegalStateException(since Fix/progress service run #4203; before that, a rawSWTException);ProgressServiceImplthrows a rawSWTException.Code that gets "some
IRunnableContext" therefore cannot be given the progress service if it might run in a background thread. Clients work around this today. Examples:ReorgCorrectionsSubProcessor,AddGetterSetterAction,OverrideMethodsAction,AddDelegateMethodsAction, ...if (window == null) context = new BusyIndicatorRunnableContext();(a hiddenDisplay.getCurrent() == nullcheck)ImportsFixNullPointerException"called from a non-UI thread" and falls backTeamOperationJob-based context instead of the progress serviceWhy the inconsistency exists
This is my reading of the git history. Confidence is given per point.
IProgressServiceextendIRunnableContext. It implementsProgressManager.runwith aProgressMonitorJobsDialog, which creates a Shell in every branch. Sure: the UI-thread dependency came with the first implementation.IRunnableContext("the current thread is used"), with no UI restriction. In the same commit, the dialog and window implementations (ProgressMonitorDialog,ApplicationWindow,WizardDialog,IWorkbenchWindow) document that withfork == false"the runnable will run in the UI thread", so they assume a caller on the UI thread. Sure about the text. Medium confidence about the intent.run(...)andbusyCursorWhilemust be called from the UI thread, and asked that this be documented. The fix comment says only that clarifications were added to the Javadoc after a newsgroup discussion about its lack of detail. Nobody discusses theIRunnableContextcontract or a reason for the restriction. High confidence: the sentence documents the behavior of the dialog-based implementation as observed in testing. Nothing in the bug shows a decision to narrow the supertype contract. The newsgroup discussion is not linked, so I could not read it.runInUI(...)has always worked from any thread, because it usessyncExec.What it does
run(fork, cancelable, runnable)can now be called from any thread, for both the workbench (E3) and the E4 progress service.forkfalsetrueFrom a thread other than the UI thread:
cancelablehas no effect that the user can see.ModalContextreports them, the same as for any otherIRunnableContext:RuntimeExceptionorErrorarrives wrapped in anInvocationTargetException.OperationCanceledExceptionarrives as anInterruptedException.runInUI(...)already worked from any thread. The class Javadoc now says so, with a warning about deadlocks. Nothing changes for calls from the UI thread.Up to now, every released version (since 2004) has thrown an
SWTException("Invalid thread access") whenrun(...)is called from a thread other than the UI thread. Since #4203 (first shipped in the 4.42 milestones M2 and M3, not yet in a release) the workbench service no longer throws theSWTException. It throws anIllegalStateExceptioninstead, with theSWTExceptionas its cause. If #4203 is part of the next release, consumers that catch theSWTExceptionmust change anyway, whether or not this PR is merged, because the exception type changed. (The E4 service still throws the rawSWTException.) With this PR such calls succeed. Code that took the old failure into account, deliberately or not, changes behavior without being recompiled:run(...)off the UI thread (for examplewindow == null ? new BusyIndicatorRunnableContext() : window, aDisplay.getCurrent()check,runInUI)OverrideMethodsAction,PasteAction,ImportsFix), EGit (CompareUtils), PDE (InputContextManager). None of them reachesrun(...)off the UI thread.ProgressServiceView, JDTTestMoveDescriptorAction, JUnitUpdateTestSuite)ProgressServiceTest(#4203), which this PR rewritesrun(...)off the UI thread and lets it fail (for example in aJobthat only logs the error)CopyFilesAndFoldersOperation.copyResourcesInCurrentThread(public API since 3.2)run(...)off the UI thread with a runnable that touches widgetsInvocationTargetException(causeSWTException) at the first widget access. Side effects before that point have already happened.run(...)inside a runnable forked by a UI-threadrun(...)ModalContextthread (asModalContextdoes it), with its own monitor. Progress is not shown in the outer dialog, and the outer dialog's cancel is not seen.ImportsFix, is stopped by its own guard)Jobwith a rule or insideIWorkspace.run) and callsrun(true, ...)with a runnable that needs the same rule and does not transfer it (IThreadListener)ProgressMonitorDialogorModalContextwhen the UI thread owns the rule. Usefork == false. A rule acquired withbeginRulecan also be moved to the forked thread inIThreadListener#threadChangewithIJobManager#transferRule; the rule of the runningJobitself cannot be moved. (WorkspaceModifyOperationdoes not help here: itsthreadChangethrowsIllegalStateExceptionwhen the currentJobowns a rule.)run(...)call from aJobor other background thread found)* Searched: eclipse.platform.ui, eclipse.platform, eclipse.jdt.ui, eclipse.jdt.debug, eclipse.jdt.core, eclipse.pde, EGit, JGit (117 progress-service call sites). Code outside these projects was not checked.
B: the only kind of code that must be rewritten to keep its behavior. None was found in the TLPs. Example:
After this PR the first call succeeds, so the fallback never runs and the user sees no dialog. To keep the dialog, call
display.syncExec(() -> runWithDialog(op))directly, without thetry/catch.D: code that suddenly starts to work. The Javadoc of
CopyFilesAndFoldersOperation.copyResourcesInCurrentThread(added in 2005 for Bug 112251, "CopyFilesAndFoldersOperation needs to remove its dependency on the UI Thread") recommends calling it from aWorkspaceJob. From there it has always thrown, because it callsgetProgressService().run(false, true, op). With this PR it works as documented. Its overwrite query and error dialogs already usesyncExec. A consumer that has called it from aJobfor years and only logged the error will now actually copy resources, without changing a line. No caller exists in the TLPs. A generic example of the same pattern:A: workarounds keep working as they are. A consumer that guards the call keeps using its fallback, because the guard is still true off the UI thread. If such a consumer calls
run(...)off the UI thread anyway (for example after removing the guard), the call now succeeds without a dialog, as described in D.G: code that used to fail fast can now hang. Example:
Assessment. Nobody has to rewrite working code, unless it follows pattern B. Pattern B was not found in the TLPs and cannot be ruled out elsewhere. Code following pattern D starts to do work that it never did before. Code following pattern G used to fail fast and can now hang. Please weigh these before merging.
Technical concept
From a thread other than the UI thread (
Display.getCurrent() == null), no dialog is involved, andforkkeeps the meaning thatIRunnableContextgives it:fork == false: the runnable runs in the calling thread ("the current thread is used to run the runnable").fork == true: the runnable runs in a new, separate thread, and the calling thread waits until it has finished. A runnable that implementsIThreadListeneris told about both thread changes, asModalContextdoes it. If the calling thread is interrupted while it waits, the interrupt is passed on to the runnable's thread, the caller keeps waiting, and afterwards it gets its interrupt flag back, unless it throwsInterruptedException.ModalContext, a caller that already runs in a forked thread (aModalContextthread, or a thread forked by a progress service) does not fork again. The runnable runs in that thread, so it keeps access to the scheduling rules and locks that thread owns. Both services use one internal JFace implementation (org.eclipse.jface.internal, friend access only), so this also holds when a runnable of the E3 service calls the E4 service, or the other way round.The separate thread is a plain thread, not a
ModalContextthread. AModalContextthread ends withdisplay.syncExec(...), so a background caller would deadlock while the UI thread waits for it (for example inJob.join()or on a lock). It would also updateModalContext's static modal level from a second thread. A test checks that the call completes while the UI thread is blocked.Other
IRunnableContextimplementations are not touched. Only the two progress services change, and only for callers off the UI thread, where they always failed before.A thread that owns a different
Displaystill takes the dialog path. The Javadoc says "a thread that is not an SWT user-interface thread", which describes this exactly. If the code checked for the workbench display instead, the behavior of the real UI thread would change during startup and shutdown.The new off-UI-thread tests are registered in
ProgressTestSuite, which CI runs as part ofUiTestSuite.How to test
Locally
org.eclipse.ui.examples.job.Window > Show View > Other... > Progress Examples > Progress Service.fork/cancelablecombination.IllegalStateException.Automated
AbstractProgressServiceOffUIThreadTestchecks the calls from a thread other than the UI thread.ProgressServiceOffUIThreadTestruns it against the E3 service,E4ProgressServiceOffUIThreadTestagainst the E4 service:fork == falsethe runnable runs in the calling thread. Withfork == trueit runs in a separate thread. In both casesrun(...)returns only after the runnable has finished, also when the caller is interrupted while it waits.IThreadListeneris notified of the thread changes whenfork == true.RuntimeException,Error,InvocationTargetException,InterruptedException,OperationCanceledException).ModalContextthread works, for both values offork.ProgressServiceTestchecks the calls from the UI thread, including when the dialog opens.ProgressTestSuite.mvn -Pbuild-individual-bundles verify -pl tests/org.eclipse.ui.tests -am -Dtest=ProgressTestSuiteon Windows. ✅ 87 tests, 0 failures, in two runs.🤖 Generated with Claude Code