Skip to content

fix: clean up background tasks on close#514

Open
agoose77 wants to merge 1 commit intojupyterlab:mainfrom
agoose77:fix-close-background-tasks
Open

fix: clean up background tasks on close#514
agoose77 wants to merge 1 commit intojupyterlab:mainfrom
agoose77:fix-close-background-tasks

Conversation

@agoose77
Copy link

@agoose77 agoose77 commented Nov 13, 2025

This PR attempts to close #503 and fix #161 by cleaning up background tasks upon closure of the websocket. I'm not sure if this fits into the wider design space for resource management, but it does fix "hang on shutdown" on my system.

@github-actions
Copy link
Contributor

Binder 👈 Launch a Binder on branch agoose77/jupyter-collaboration/fix-close-background-tasks

@agoose77 agoose77 added the bug Something isn't working label Nov 13, 2025
@davidbrochart
Copy link
Collaborator

That would prevent cleaning up the room that was launched here, right?

@agoose77
Copy link
Author

I don't think so? That is launched by asyncio's create_task, rather than the class helper.

@davidbrochart
Copy link
Collaborator

Ah right.
So it looks like you are also cancelling the WebSocket server (launched here), while you only want to cancel this particular handler)?

@krassowski
Copy link
Member

I edited description as I closed #503 as a duplicate of #161

@agoose77
Copy link
Author

agoose77 commented Jan 2, 2026

It looks as though there's already some consideration for cleanup in

# FIXME some clean up should be upstreamed and the following does not
# prevent hanging stop process - it also requires some thinking about
# should the ystore write action be cancelled; I guess not as it could
# results in corrupted data.
# room_tasks = list()
# for name, room in list(self.rooms.items()):
# for task in room.background_tasks:
# task.cancel() # FIXME should be upstreamed
# room_tasks.append(task)
# if room_tasks:
# _, pending = await asyncio.wait(room_tasks, timeout=3)
# if pending:
# msg = f"{len(pending)} room task(s) are pending."
# self.log.warning(msg)
# self.log.debug("Pending tasks: %r", pending)

Do we know why this was committed but commented out?

@agoose77 agoose77 force-pushed the fix-close-background-tasks branch from 1ffd612 to 3b7cbcc Compare January 2, 2026 16:13
@krassowski
Copy link
Member

It looks like tests did not run because pre-commit failed:

check builtin type constructor use.......................................Failed
- hook id: check-builtin-literals
- exit code: 1

projects/jupyter-server-ydoc/jupyter_server_ydoc/websocketserver.py:66:21: replace list() with []

trim trailing whitespace.................................................Passed
ruff.....................................................................Passed
ruff-format..............................................................Passed
doc8.................................................(no files to check)Skipped
mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

projects/jupyter-server-ydoc/jupyter_server_ydoc/websocketserver.py: note: In member "clean" of class "JupyterWebsocketServer":
projects/jupyter-server-ydoc/jupyter_server_ydoc/websocketserver.py:68: error:
"YRoom" has no attribute "background_tasks"  [attr-defined]
                for task in room.background_tasks:
                            ^~~~~~~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 28 source files)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: No status

3 participants