IllegalStateException: "Cannot set a new parent without first clearing the

I just have been hit by this error with vaadin 6.8.4 (and 6.8.5),
here a short summary in case someone else also “jumps” into this problem :open_mouth:

IllegalStateException: "Cannot set a new parent without first clearing the old parent"

This is a error message you usually don’t see, and it does not show up in the server console.

Here the (simplified) screen layout I had:

VerticalLayout1
->HorizontalLayout1->Picture1
->HorizontalLayout2->Label1
->HorizontalLayout3

On some events I did remove all Childrens from VerticalLayout1 and then did regenrate the content based on the new situation.
The Picture1 was also removed and added a new Picture.
The Label1 was only once generated on creation of the layout and then readded to HorizontalLayout2 when needed.

So far everything worked fine.
For optimization purpose I did refactor the event routine to be processed in a background task and the push the changes via the ProgressBar component.

And here the troubles started.
On some (not all) events the Picture1 was still visible, even as I had removed it with a call to removeAllComponents() on VerticalLayout1…:blink:
On the next mouse click in the screen/layout I then had a almost empty screen.

Crazy, when I did a refresh (F5) from teh browser everything looked as it should.

My first guess was that a synchronized(getApplication()) was missing, but of course (after having checked them over 10 times) I had all required synchronized(getApplication()) correctly in place… :mellow:

I did then (after many other tests/checks) start the application with ?debug in the browser window.
Here it then finally show the error message:

IllegalStateException: "Cannot set a new parent without first clearing the old parent"

After a few more analysis it was clear that removing the Label1 indirectly via the removeAllComponents() on VerticalLayout1 did not completely remove it from the client side tree.
And when the Label1 was later on added again, the client did not accept it with the message mentioned.
Normally such conditions are handled on server side with good error messages, but in this case on server side just nothing got indicated.
I assume this is due to the fact that I manipulate the component tree from several Threads.

The solution was then to create a new Label each time and not reuse a old one which might still have some (inactive) parents. :ph34r:

Strange enough, the error message is indentic to this ticket:
http://dev.vaadin.com/ticket/8786