UI.access not working

I’ve been doing battle trying to update a Label within a panel of the Vaadin Wizaard.
Basically when I try to change the value of a Label from a background thread the UI locks up (the dreaded red progress indicator).

So my original code was:

	synchronized (UI.getCurrent())
	{
		UI.getCurrent().access(new Runnable()
		{
			@Override
			public void run()
			{
				progressDescription.setValue("Imported: " + count +
				 " records.");
			}
		});
	}

This does a great job of locking the UI up with the red progress indicator.

So eventually I tried this:
synchronized (UI.getCurrent())
{
UI.getCurrent().access(new Runnable()
{
@Override
public void run()
{
try
{
VaadinSession.getCurrent().getLockInstance().lock();
progressDescription.setValue(“Imported: " + count +
" records.”);
}
finally
{
VaadinSession.getCurrent().getLockInstance().unlock();
}

			}
		});
	}

Now everything works fine.

The original code fails for both vaadin 7.1.3 and 7.1.4.

So have I mis-understood how I’m meant to use the access method?

I ended up writting a little helper class to get around this problem until vaadin have fixed it. Thought I would post it he for others.

BTW its fairly easy to reproduce this problem by setting up an ProgressIndicator inside a vaadin wizard and then updating the ProgressIndicator from a background thread.
I’m guessing that the ‘access’ method works OK if it can run the Runnable immediately (e.g. if the session is not currently lock) but if it has to schedule the runnable then its failing to take the lock at the point in time that the runnable is called.

Anyway here is the class:


public class UIUpdater
{
	private static Logger logger = Logger.getLogger(UIUpdater.class);

	public UIUpdater(final Runnable uiRunnable)
	{
		UI.getCurrent().access(new Runnable()
		{
			@Override
			public void run()
			{
				try
				{
					VaadinSession.getCurrent().getLockInstance().lock();
					uiRunnable.run();
				}
				catch(Throwable e)
				{
					logger.error(e,e);
					Notification.show(e.getClass().getSimpleName() + ":" + e.getMessage(), Type.ERROR_MESSAGE);
				}
				finally
				{
					VaadinSession.getCurrent().getLockInstance().unlock();
				}

			}
		});

	}

}

If you currently have:


	protected void taskComplete(final int sent)
	{
		UI.getCurrent().access(new Runnable()
		{

			@Override
			public void run()
			{
				listener.taskComplete(sent);
			}

		});

	}


Then change it to:


	protected void taskComplete(final int sent)
	{
		new UIUpdater(new Runnable()
		{

			@Override
			public void run()
			{
				listener.taskComplete(sent);
			}

		});

	}

Hi, do you get any errors in the server log? Do you have assertions enabled in the JVM? The method running the pending tasks (VaadinService.runPendingAccessTasks) asserts that the lock is held - there are other useful asserts as well that might fail if there’s something amiss. Taking a look at the code, it certainly seems that all code paths calling the method also take care of the locking. I’m fairly sure this one is some sort of a corner case - if it were more common, we and/or others would have run into it as well.

This seems a bit suspicious; it shouldn’t be necessary and might easily lead to a deadlock between the VaadinSession lock and the UI monitor.

No errors in the server log.
No, I’m not using any asserts in the jvm. Are you suggesting that I should enable them ?

I should note that the easy way to reproduce this error is to put a ProgressBar inside the vaadin wizard which also has a ProgressBar.

I will test your thoery about the ‘synchronised’ possibly causing a deadlock and post the results back here.

Yes; as of 7.1, Vaadin includes quite a few asserts (especially related to communication and concurrency) and it could be very useful to enable them whenever debugging potential Vaadin issues.

Hi,

I had two issues with UI.access() too.

1.) When the progress is faster than the poll interval the UI hanged.
2.) After a while running the server 1-2 days suddenly the UI hanged too when trying to access the UI.

Now I am using Brett’s workaround (thanks!) and at least the first issue does not exist anymore. So I don’t have to use a poll interval of 50 mili seconds, haha! Hopefully this will clear the second issue too.

I work with the ProgressBar too but with vaadin 7.3

I’m still quite new at everything to do with programming… but I got my first deadlock a few weeks ago. So this might be common practice for the more experienced. But for what it’s worth:

jstack -F <PID> 

Where is the process ID of java. The resulting stack traces dumped to the log clearly identify who’s waiting for who, and how they got there. My deadlock was my fault, of course. :slight_smile: