Should not dispose container context in container.close()
Describe the bug
When the container is closed, its context is disposed. One effect of this is that all handlers registered on the deltaManagerProxy are removed.
this.context?.dispose();
This line from container.ts was added in the fix for #1313.
I discovered this while adding support to listen to the 'readonly' event from a component. https://office.visualstudio.com/DefaultCollection/OC/_git/office-bohemia/pullrequest/444045. That PR is blocked on this issue. I am reproing in the PR by creating a new document, removing it from OneDrive, waiting approx 1min for an error event to fire, and expecting my readonly handler to be called.
At first, the App was exposing this issue because it was explicitly closing the container during onerror, its handler for readonly was getting unregistered before ever being called. But after removing the unnecessary app code to close the container, the problem is still there in the runtime.
The code flow is: reconnectOnError -> connectCore -> deltaManager.close() -> deltaManager closed event -> container.close() (due to below code) -> deltaManager.close() (early return) -> context?.dispose () -> handlers on deltaManagerProxy are removed before they are triggered by code in deltaManager.close() which hasn't run yet
private attachDeltaManagerOpHandler(attributes: IDocumentAttributes, catchUp: boolean): void {
this._deltaManager.on("closed", () => {
this.close();
});
Logging after discussing with @vladsud
Basically we need to ensure that all events are delivered to consumers on container close. Right now "readonly" event is raised at the very bottom of Container.close() when container runtime already killed all listeners.
@vladsud The container calls removeAllListeners on itself at the end of close(), so would moving dispose to after that be fine here? (Assuming "its handler" refers to the component's in "as a result, its handler for readonly was getting unregistered " above). The bug title suggests that we should not dispose the context at all.
(Posting here as well for documentations) is there an issue with switching the calls to setReadonlyPermissions and emit("closed") in deltaManager? if it's actually just wrong to close/dispose the ContainerContext during Container.close i can fix that, but the Components need to know about Container close so that would result in two different close paths in ContainerContext - one to jsut pass along Container.close and one to actually close
Changes into 0.15 get (semi)automagically integrated back to master, right? Closing because Vlad's change sounds like it addresses the issue.