automerge/automerge-repo

Timeout in DocHandle waitFor prevents process from exiting.

dmaretskyi opened this issue · 3 comments

The waitFor helper called here is creating a setTimeout that doesn't get cleared which prevents the node.js process from exiting.

They can be seen when running wtfnode after creating documents with automerge.

I believe this could be fixed by adding another final CLOSED state to DocHandle and a method to Repo that transitions all doc handles into that state.

sroze commented

I also had this issue of having automerge-repo processes not stopping with NodeJS. For each Repo I have created, I need to do the following to close down all intervals & sockets:

// Create your `Repo`
const adapter = new BrowserWebSocketClientAdapter(`ws://localhost:${PORT}`);
const repo = new Repo({
  network: [ adapter ],
  // ...
})

// I disconnect `Repo` from the server:
adapter.disconnect()
adapter.peerId = undefined; // if not done, it will reconnect automatically.
adapter.socket?.close();

// stop each handle
Object.values(repo.handles).forEach((handle) => {
  // @ts-ignore `stop` has been added manually through a patch
  handle.stop();
});

For context, here's DocHandle's stop method that I had to add through a patch:

stop() {
  this.#machine.stop();
}

Provided guidance and strong PoV on the right technical strategy, very happy to contribute the changes needed.

sroze commented

@dmaretskyi @pvh it would be great to have your insights on this.

pvh commented

ah yeah, this is just unfinished work i haven't felt enough pain from to fix yet.

a repo.stop() would be a good short-term workaround but really we probably also want something like repo.allSynced() or something as well.

broadly speaking the design here is something along the lines of "for short-lived processes there should be a way to gracefully exit when a task is done" and i think the "when a task is done" is going to be the more involved part.