td icon indicating copy to clipboard operation
td copied to clipboard

Web - the TDLib stops working after the initial sign-in (Repro included)

Open kubk opened this issue 6 months ago • 11 comments

Hi, thank you for implementing this library! I am looking forward to use it on Web but stumbled upon the issue: sign in works correctly and after that I am able to fetch the info about the current user or fetch my chats. But if I refresh the page, the Telegram API stops working and I get the following error: [NotificationManager. cpp: 4006][NotificationManager] Check ' running get_difference_' failed

The issue can be reproduced here: https://github.com/kubk/tdlib-refresh-bug (no bundlers, no frameworks, single demo.html file)

What I noticed:

  • The issue can be seen on different TDLib versions. The tdlib in the repo above is up to date and built from sources today. I also faced the same issue with 1.8.40 from @dibgram.
  • I see the same issue on Mac and Linux, and in Chrome and Safari.
  • I get this issue when I use a bundler with React and when I use plain JS with no bundlers.

Steps to reproduce:

  • Clone the repo, install dependencies via npm install and run the project using npm run serve . Open http://localhost:5177/demo
  • Sign in using your phone number
  • After successful login, click on the get me button. It will show your username via alert
  • Refresh the page. Sign in is no longer needed (because TDLib cached auth data?) and there is an error in the console. The get me button no longer works. If the issue is not reproduced, try refreshing the page a couple of times.

kubk avatar Jun 04 '25 17:06 kubk

Could you increase log verbosity level to 4 using the option logVerbosityLevel=4 of TdClient, reproduce the issue, save the log as a file and send it to https://t.me/tdlib_bot?

levlam avatar Jun 05 '25 09:06 levlam

@levlam Done.

Also, it seems like I accidentally found a really strange fix for the issue. I replaced this:

initializeTdClient()

with this:

initializeTdClient().then(getTdlibVersion)

function getTdlibVersion() {
  sendQuery({
    "@type": "getOption",
    "name": "version"
  }).then((res) => {
    document.getElementById('version-display').textContent = `TDLib version: ${res.value || 'unknown'}`;
  }).catch(err => {
    console.error("Error getting TDLib version:", err);
    document.getElementById('version-display').textContent = `TDLib version: Error getting version`;
  });
}

And it somehow fixed everything. Maybe it will help you with debugging: https://github.com/kubk/tdlib-refresh-bug/commit/dff25b26a45cc9566238118bfca49d0f6ec37bdf

Also, you can replace the compiled TDlib version with your own if you do not trust mine. I intentionally made it simple to swap out, with no bundlers or frameworks involved.

kubk avatar Jun 05 '25 09:06 kubk

@levlam Video proof attached

https://github.com/user-attachments/assets/5dd6cbb0-587b-4b0e-b630-6ec068a2f5dc

kubk avatar Jun 05 '25 10:06 kubk

I've tried a different Telegram account (sorry for not checking it earlier) and the issue can't be reproduced there at all. So it also depends on the account. I can reproduce it only on user ID 6698689434 and if I call initializeTdClient() instead of initializeTdClient().then(getTdlibVersion)

kubk avatar Jun 05 '25 10:06 kubk

Unfortunately, I started getting an error on both accounts. This time, it is a different one. It did not appear immediately, the Telegram session was a few hours old.

Image

The log file has been sent to the Telegram bot

kubk avatar Jun 05 '25 14:06 kubk

Both errors look impossible. MpscPollableQueue must never be used at all in tdweb and the crash can signal some miscompilation issue. Did you use emsdk 3.1.1 to build tdweb?

levlam avatar Jun 05 '25 21:06 levlam

@levlam Yes, I did use 3.1.1 to compile on Linux, all the steps were taken from here and here

The issue is that at the moment there's no working tdlib example for web. Both evgeny-nadymov/telegram-react and DIBgram use outdated version of tdlib and I couldn't go through auth there: https://github.com/DIBgram/DIBgram/issues/10

The author of DIBgram published his own version of tdlib that has the same issues after the page refresh: https://github.com/DIBgram/tdweb . Their build process is automated

Would it be possible for you to publish an officially compiled TDLib for Web build, at least once? This would help determine whether the issue stems from my compilation process or is inherent to the library itself

kubk avatar Jun 06 '25 04:06 kubk

@levlam Hi, just following up on this one. I tried the official DIBgram build and I encountered the same issues. So either there is an issue with TDLib for web or both the DIBgram author and I built tdlib the wrong way.

More details:

  • repro with DIBgram's tdlib: https://github.com/kubk/tdlib-refresh-bug/commit/576db4213e5bba3d1def6bc61ce42c65c4e72653 . The repo is just index.html so you can easily replace tdweb.js, .wasm and 2 .worker.js files with your own versions if you don't trust mine.
  • The issue isn't reproducible right away. I successfully login and everything is working. Then if I refresh the page OR wait 10 minutes and refresh the page - I start getting errors like "Fatal error: [ 0][t 0][1750750121.628999948][NotificationManager.cpp:4168][!NotificationManager] Check running_get_difference_ failed" or "Fatal error: Error: memory access out of bounds".

So the issue is only reproducible when TDLib remembers an authenticated user and some time has passed. I was hoping to use tdlib for a new Telegram client because tdlib implements sync and persistence correctly, but it seems like I'll have to recreate it from scratch the same way it works in webk/webz.

kubk avatar Jun 24 '25 13:06 kubk

@kubk Could you try a different browser?

levlam avatar Jun 24 '25 22:06 levlam

@levlam I tried Safari instead of Chrome. I was able to reproduce the error, but only 8-9 hours after a successful sign-in. Repro scenario:

  • Open the app https://github.com/kubk/tdlib-refresh-bug
  • Sign in. It always works (2FA is supported). After that you click "Get me" to get information about signed in user.
  • Refresh page a couple of times. In my case with Safari I had to wait around 9 hours. With Chrome it was reproducible in like ~10 minutes
Image

Since the error is fatal, tdlib completely stops working after that. Maybe the waiting time isn't important and everything is just random. But unfortunately, I can't build a product on top of this. To summarize, the issue is reproducible for me on both Telegram accounts, on both Mac and Linux, on Chrome and Safari, on both DIBgram build and my own with the latest version.

Anyway, I've decided to fork Webz instead. Let me know if you need more details from me.

kubk avatar Jun 25 '25 14:06 kubk

Thank you. You provided already much more details than I would have expected.

levlam avatar Jun 25 '25 16:06 levlam