question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unresolved promises after disconnect resulting in race condition.

See original GitHub issue

After 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:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
painorcommented, Jan 27, 2022

I’ve pushed a small fix to 2.4.5. it might fix it.

0reactions
voxsoftwarecommented, May 20, 2022

JS doesn’t allow you to cancel a promise so we can’t really kill it after disconnect.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with race condition on custom command
This seems to be prone to the race condition where MY_ACTION can be triggered (as a result of cy.visit(); ) before the listener...
Read more >
Concurrency, part 3: Promises – Asynchronous programming ...
An unresolved promise has no value yet, and instead stores a list of callbacks to run once the promise is resolved.
Read more >
Promise.race() - JavaScript - MDN Web Docs
Unlike other promise concurrency methods, Promise.race is always asynchronous: it never settles synchronously, even when the iterable is empty.
Read more >
Global Trends 2030: Alternative Worlds
It is our contention that the future is not set in stone, but is malleable, the result of an interplay among megatrends, game-changers...
Read more >
Closing the Water Access Gap in the United States
sanitation are baseline conditions for health, ... a result, water- and sanitation-related diseases ... that compare access to plumbing by race and.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found