nicegui: Confusing desync with blocking calls
Description
Doing blocking calls in some places (e.g. in event handlers) can sometimes cause silent desync between the browser and Python.
For example, take this code:
import time
from nicegui import ui
counter = 0
def increment():
global counter
counter += 1
time.sleep(5)
label.text = counter
log.push(f'Increment {counter}')
ui.button('Increment', on_click=increment)
ui.button('Print', on_click=lambda: log.push('Print'))
label = ui.label('-')
log = ui.log()
ui.run()
Here is an example of me playing around with this code:
https://github.com/zauberzeug/nicegui/assets/5256211/b828f131-ccd6-4b4e-99fa-2ce4333c5d6a
Note in particular two things:
- After the first increment completes, the GUI works, but the counter and log are out of sync with the actual state in Python (which is revealed when reloading the page).
- There is no indication that anything has gone wrong. The desync occurs completely silently. (Sometimes a ‘Connection lost’ message pops up briefly, but in this case it didn’t. Whether it shows up or not seems to be random.)
Now obviously you’re not supposed to use long-running blocking calls in event handlers, because it blocks the async event loop. However, the problem is that if you accidentally introduce a blocking call then it can lead to silent desync which is very confusing and hard to debug (speaking from experience).
In my opinion, there are at least two issues here:
- These desyncs can occur with no indication that anything has gone wrong.
- Even after the system recovers from the blocking call and resumes working as normal the desync is not corrected.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 15 (14 by maintainers)
I’ve got the message history/retransmission pretty much done. I hugely overestimated how much time it would take. I’m going to blame it on how easy it is to work in this codebase. 😉
Unfortunately, “Connection state recovery” doesn’t appear to be implemented for the Python version: https://github.com/miguelgrinberg/python-socketio/issues/1258
“Connection state recovery” looks interesting: https://socket.io/docs/v4/connection-state-recovery https://socket.io/docs/v4/tutorial/step-6
I’m surprised to see this because on another page of the documentation, it says if you want this kind of functionality, you have to implement it yourself. I’ll have to take a close look at this later.
Hi @FeldrinH,
That’s an interesting observation. If I understand correctly, “desync” basically means that the UI is not updated after
increment()has been called, right? All log messages - if displayed after reload - are in the right order.So it boils down to something like this:
With a delay of 4 seconds the UI is correctly updated, but not with 5 seconds.
I guess what is happening is that the socket connection disconnects while the server is sleeping, but afterwards the server automatically reconnects. Until the reconnect is done, updates get lost.
We’ll need to investigate this more thoroughly to see if we can do anything about it.