Open johannest opened 1 week ago
A few notes:
Component.readObject
might not be neededwriteMethod
could be
private void writeObject(java.io.ObjectOutputStream stream)
throws IOException {
if (this instanceof UI ui) {
Map<Class<?>, CurrentInstance> old = CurrentInstance.setCurrent(ui);
try {
stream.defaultWriteObject();
} finally {
CurrentInstance.restoreInstances(old);
}
} else {
stream.defaultWriteObject();
}
}
Actually, calling CurrentInstance.setCurrent(ui)
when deserializing the UI instance would fail because if calls internals.getSession()
, but internals
field is not yet set.
Description of the bug
This issue is tightly connected to kubernetes kit issue: https://github.com/vaadin/kubernetes-kit/issues/140
While discussing with @mcollovati he discovered that as a part of fixing the issue above, there is a need for a fix also in the Flow:
"We need to set the UI threadlocal. Unfortunately we cannot do it in the UI class, because the reference to the bean is held by its parent class Component that is serialized before UI.writeObject() gets called. But we can "fix" this in Flow by adding the thread local logic in Component class. Once this is done, the serialization completes correctly (also needs setting threadlocal in VaadinSession writeObject)"
Expected behavior
Serialization and the de-serialization works also when the application contains scoped beans (@SessionScope, @UIScope, @RouteScope).
Minimal reproducible example
See https://github.com/vaadin/kubernetes-kit/issues/140
The Flow fix could be something along these lines.
com.vaadin.flow.component.Component. Add a Thread Local logic:
VaadinSession improve the existing writeObject with a Thread Local handling:
Versions