nicegui: Large websocket messages cause error that is silently caught somewhere
Description
Description
When ui.textarea contains more than a million characters, it starts to behave unpredictably without any visible warnings.
Steps to Reproduce
To demonstrate the issue from different perspectives, I wrote the following code:
from nicegui import ui
@ui.page('/')
def main():
text_area = ui.textarea('Type something').props('rounded outlined input-class=mx-3') \
.classes('w-full self-center')
text_area.value = '123' + 'A'*1_000_000
len_label = ui.label('Length').classes('mt-2')
def text_area_length():
print('Length:', len(text_area.value))
len_label.set_text(f'Length: {len(text_area.value)}')
print('first_character:', text_area.value[0])
def text_area_delete_first():
text_area.value = text_area.value[1:]
text_area_length()
ui.button('Calculate', on_click=text_area_length)
ui.button('Delete first character', on_click=text_area_delete_first)
text_area_length()
ui.run(title='Text Input Max')
When you run this code, it creates a textarea filled with over a million characters, plus three more to make the behavior more evident in different scenarios.
- If you click the
Calculatebutton immediately after launching (without interacting with theui.textarea), everything is calculated correctly, and the console prints the appropriate values. - If you add or delete a character using the
ui.textarea(e.g., delete the digit1at the beginning or add a new character), theCalculatebutton will no longer calculate the new value oftext_areacorrectly. The code executes without errors, and you see the print output, but it shows the wrong number of characters and the first character is also incorrect, as if the connection between the frontend and backend is lost. - To further investigate, I created a button to delete the first character without using the cursor to interact with the
ui.textarea. In this case, everything works as expected, the counter decreases, and the first characters are read correctly.
Additional Investigation
To see if this is a Quasar issue, I added character counting to their textarea code: https://codepen.io/Xtreemrus/pen/XWLNPyY?editors=101
<!--
Forked from:
https://quasar.dev/vue-components/input#example--textarea
-->
<div id="q-app" style="min-height: 100vh;">
<div class="q-pa-md" style="max-width: 300px">
<q-input
v-model="text"
filled
type="textarea"
></q-input>
<div class="q-mt-md">
<p>Calculate len: {{ characterCount }}</p>
<p>first_character: {{ firstCharacter }}</p>
</div>
</div>
</div>
JS part:
const { ref, computed } = Vue
const app = Vue.createApp({
setup () {
const text = ref('')
const characterCount = computed(() => text.value.length)
const firstCharacter = computed(() => {
if (text.value.length > 0) {
return text.value[0]
} else {
return 'No symbols'
}
})
return {
text,
characterCount,
firstCharacter
}
}
})
app.use(Quasar, { config: {} })
app.mount('#q-app')
In this case, working with a million characters is handled correctly.
Impact
A couple of years ago, cases requiring a million characters in a textarea were rare. However, with the development of LLMs, users are inserting increasingly large chunks of information into input fields. One million characters are approximately 250,000 tokens, which is now a feasible amount.
Difficulty
The challenge with this bug is that no visible errors occur, neither on the user side nor the developer side. There are no exceptions thrown, making it hard to find a workaround.
Environment
- Browser: Chrome
- NiceGUI version: 1.4.30
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 3
- Comments: 25 (12 by maintainers)
For reference, here is a demo showing
Here goes nothing: https://github.com/miguelgrinberg/python-engineio/issues/397
OK. I came to the conclusion that Python.EngineIO does not have method for setting any extra handler for a too-long WebSocket message. It just throws it silently out the window, terminates the connection, and calls it a day. Zero chance of attaching a handler without modifying upstream.
Look at this:
https://github.com/miguelgrinberg/python-engineio/blob/527e2a944985fc7f63a28169dfe8f4b325287885/src/engineio/async_socket.py#L151-L157
the inner function
websocket_waitraisesValueError('packet is too large')if too long.Then here:
https://github.com/miguelgrinberg/python-engineio/blob/527e2a944985fc7f63a28169dfe8f4b325287885/src/engineio/async_socket.py#L220-L241
You see, if websocket_wait raises an exception, it is caught by the catch-all
except:block, which simply breaks the while loop of listening in on WebSocket messages.Then finally here:
https://github.com/miguelgrinberg/python-engineio/blob/527e2a944985fc7f63a28169dfe8f4b325287885/src/engineio/async_socket.py#L258-L261
It then shuts the WebSocket like usual, as if nothing happened.
The above is confired by strategically placing print statements (idk
pdb, sorry)We then see the following in the terminal when the too-long message is sent:
I would call it bad code quality and API design on their part…
Interesting 🤔
Would say, though: maybe a solution would be to send the difference in string, when the textarea is updated? Then no need to send so many characters across the internet?
But that would be quite difficult and require an intensive rework …