Bug 272859 - [Workbench] Exception thrown by syncExec executed in a Job should throw SWTException/ERROR_FAILED_EXEC
Summary: [Workbench] Exception thrown by syncExec executed in a Job should throw SWTEx...
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Runtime (show other bugs)
Version: 3.4.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: platform-runtime-inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-04-20 03:50 EDT by Clovis CLA
Modified: 2023-10-05 03:05 EDT (History)
5 users (show)

See Also:


Attachments
Example Project (8.42 KB, application/x-zip-compressed)
2009-04-22 12:03 EDT, Kevin Barnes CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Clovis CLA 2009-04-20 03:50:43 EDT
The documentation of syncExec says that an SWTException is thrown if an exception occurs while executing the Runnable. This does not happen if you invoke syncExec inside a Job.

For the following snippet:
Actual result: Output is "done, no exception"
Expected result: Output is "caught: class org.eclipse.swt.SWTException"

new org.eclipse.core.runtime.jobs.Job("job") {
    protected org.eclipse.core.runtime.IStatus run(org.eclipse.core.runtime.IProgressMonitor monitor) {
        try {
            display.syncExec(new Runnable() {
                public void run() {
                    throw new RuntimeException();
                }
            });
            System.out.println("done, no exception");
        } catch (Throwable anyException) {
            System.out.println("caught: " + anyException.getClass());
        }
        return org.eclipse.core.runtime.Status.OK_STATUS;
    }
}.schedule();
Comment 1 Kevin Barnes CLA 2009-04-20 09:38:46 EDT
The SWTException is thrown, but it's handled by the workbench's runEventLoop function before the syncExec returns.
Comment 2 Paul Webster CLA 2009-04-22 11:19:59 EDT
(In reply to comment #1)
> The SWTException is thrown, but it's handled by the workbench's runEventLoop
> function before the syncExec returns.

We have a org.eclipse.jface.window.Window.IExceptionHandler doing a handler.handleException(t);

So this handler is called before Display#syncExec(*) has a change to propogate the exception back?  Or syncExec cannot do that at all?

PW


Comment 3 Kevin Barnes CLA 2009-04-22 11:27:08 EDT
The workbench handler gets called before the syncExec returns. A syncExec call will wake the Display and wait until the runnable has executed on the main thread before returning.
Comment 4 Paul Webster CLA 2009-04-22 11:36:54 EDT
If I take a snippet and make the dispatch loop look like ours, it works:

while (!shell.isDisposed()) {
	try {
		if (!display.readAndDispatch())
			display.sleep();
	} catch (Throwable e) {
		System.err.println("dispatch: " + e); //$NON-NLS-1$
	}
}

a thread runnable:
public void run() {
	try {
		System.err.println("go"); //$NON-NLS-1$
		display.syncExec(new Throw());
		System.err.println("no"); //$NON-NLS-1$
	} catch (Throwable e) {
		System.err.println("syncExec: " + e); //$NON-NLS-1$
	}
}


I get:
dispatch: org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.RuntimeException: There is no spoon)
syncExec: org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.RuntimeException: There is no spoon)

Comment 5 Kevin Barnes CLA 2009-04-22 12:03:22 EDT
Created attachment 132801 [details]
Example Project

Run this example project and click the new tool bar item.

Output looks like:

done, no exception

!ENTRY org.eclipse.ui 4 0 2009-04-22 12:00:44.296
!MESSAGE Unhandled event loop exception
!STACK 0
org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.RuntimeException)
	at org.eclipse.swt.SWT.error(SWT.java:3864)
	at org.eclipse.swt.SWT.error(SWT.java:3779)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:137)
	at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3855)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3476)
	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2401)
	at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2365)
	at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2217)
	at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:368)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:559)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1287)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1263)
Caused by: java.lang.RuntimeException
	at forpaul.handlers.SampleHandler$1$1.run(SampleHandler.java:33)
	at org.eclipse.ui.internal.UILockListener.doPendingWork(UILockListener.java:155)
	at org.eclipse.ui.internal.UISynchronizer$3.run(UISynchronizer.java:158)
	at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
	at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:134)
	... 23 more
Comment 6 Paul Webster CLA 2009-04-22 12:17:57 EDT
John, it is some interaction with the Job framework.

If I modify Kevin's handler to both schedule the job and start the thread (with the same syncExec try/catch block in it) the job sees no exception and the thread gets the exception.

PW
Comment 7 Paul Webster CLA 2009-05-20 15:20:41 EDT
Off to Jobs

PW
Comment 8 Eclipse Webmaster CLA 2019-09-06 16:16:45 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.