Skip to content

Conversation

@evnchn
Copy link
Collaborator

@evnchn evnchn commented Nov 9, 2025

Motivation

TL-DR: If you want something to always get cleaned up, clean it up in one place, never switch PICs. Keep logic clean and firm by always cleaning up when the function elapses - only way to stop it is to cancel the function.

So a client should, ideally, self-destruct.

But now a client is destroyed in a variety of places, and only sometimes it actually destroys.

  • Before socket connect: by Client.prune_instances
  • After socket connect: by the _delete_tasks, which can be multiple, but we act on the last one because we only actually delete when self._num_connections[document_id] == 0

This is quite some technical debt (no we are not 🤡 enough to write this on day one). Some left from 2.x when we had auto-index client. This PR seeks to eliminate it.

Implementation

Removals and deprecations:

  • No more _num_connections.
  • No more _delete_tasks (plural).
  • Deprecate Client.prune_instances (does nothing now)

Additions

  • We do a singular _delete_task. Considering the above, only the last one matter anyways, so why bother.
  • self._reset_self_delete: For share the logic between the existing socket-disconnect self-delete, and the before-connect 60-second self-delete

Progress

  • I chose a meaningful title that completes the sentence: "If applied, this PR will..."
  • The implementation is complete.
    • Pray to pytests
  • Pytests are not necessary.
  • Documentation is not necessary.
    • Possibly to warn the user if they manually call Client.prune_instances though.

@evnchn
Copy link
Collaborator Author

evnchn commented Nov 9, 2025

Wait but then would 8414e1d indicate that our code can possibly break existing user code which spawns Client before core.loop is ready, causing error like:

...
  File "/home/runner/work/nicegui/nicegui/nicegui/client.py", line 104, in __init__
    self._reset_self_delete(timeout=60.0)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/runner/work/nicegui/nicegui/nicegui/client.py", line 313, in _reset_self_delete
    self._delete_task = background_tasks.create(delete_content(), name=f'delete content {self.id}')
                        ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/nicegui/nicegui/nicegui/background_tasks.py", line 27, in create
    assert core.loop is not None
           ^^^^^^^^^^^^^^^^^^^^^
AssertionError

🤔

@evnchn evnchn added feature Type/scope: New feature or enhancement review Status: PR is open and needs review labels Nov 9, 2025
@evnchn
Copy link
Collaborator Author

evnchn commented Nov 9, 2025

The force-pushes are due to incomplete pylint fixes.

We can agree that force-push are for "commits you don't want people to see"

@falkoschindler falkoschindler self-requested a review November 10, 2025 17:05
@falkoschindler falkoschindler added this to the 3.4 milestone Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Type/scope: New feature or enhancement review Status: PR is open and needs review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants