Unresolved promises after disconnect resulting in race condition.
See original GitHub issueAfter calling the client disconnect()
command, I’ve found that a pending promise can resolve after the the disconnect resulting in the [ERROR] - [Error: Cannot send requests while disconnected. You need to call .connect()]
error.
This line sleeps for 9 seconds https://github.com/gram-js/gramjs/blob/master/gramjs/client/updates.ts#L172
before calling the timeout()
callback:
https://github.com/gram-js/gramjs/blob/master/gramjs/client/updates.ts#L180
By the time the callback occurs the client may have already disconnected. client._sender.send()
then results in the error mentioned above.
It also appears that the property _destroyed
is never set, so that this while
statement is stuck in an infinite loop.
https://github.com/gram-js/gramjs/blob/master/gramjs/client/updates.ts#L171
I’m not sure what the best solution is to fix the async race condition here. Somehow these sleep
and timeout
promises need to be canceled when the client disconnect()
is called. Or perhaps the disconnect()
method needs to wait for the pending timeout()
to resolve before resolving itself.
As a temporary workaround I’m using:
myClient._destroyed = true;
await sleep(9000);
In this way I can force the while
loop to stop and then wait the PING_INTERVAL
period to make sure the pending promises resolve.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
I’ve pushed a small fix to 2.4.5. it might fix it.
You can reject the promises. Promises never rejected or resolved are a really big problem to app, because can generate memory leaks.
For example I have situations when I try download two files at same time, Gramjs disconnects one connection while getting the data, and the promise never resolve/reject.