A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Allow secondary consumers during session startup (#11178)
Trevor Manz9043f74f62f0
diff --git a/marimo/_server/api/endpoints/ws/ws_session_connector.py b/marimo/_server/api/endpoints/ws/ws_session_connector.pyindex dd64e0e4ba..aca0db6b6f 100644--- a/marimo/_server/api/endpoints/ws/ws_session_connector.py+++ b/marimo/_server/api/endpoints/ws/ws_session_connector.py@@ -226,6 +226,7 @@ class SessionConnector:session_consumer=self.handler,file_key=self.params.file_key,auto_instantiate=self.params.auto_instantiate,+ main=True,)self._notify_kernel_ready(new_session)diff --git a/marimo/_server/session_manager.py b/marimo/_server/session_manager.pyindex bd6e410fae..3fbc0a6285 100644--- a/marimo/_server/session_manager.py+++ b/marimo/_server/session_manager.py@@ -217,8 +217,15 @@ class SessionManager:query_params: SerializedQueryParams,file_key: MarimoFileKey,auto_instantiate: bool,+ *,+ main: bool,) -> Session:- """Return a ready session, retaining ownership during startup."""+ """Return a ready session, retaining ownership during startup.++ `main=True` assigns the editing connection to the consumer.+ Secondary consumers receive updates alongside the editing client+ and can connect before an editing client arrives.+ """if self._closed:raise KernelStartupError("Session manager is shut down")key = self._connection_key(session_id, file_key)@@ -237,6 +244,7 @@ class SessionManager:query_params,file_key,auto_instantiate,+ main=main,)if pending is None:startup = SessionStartup()@@ -282,7 +290,7 @@ class SessionManager:if self._pending.get(key) is pending:self._pending.pop(key)raise KernelStartupError("Session closed during startup")- if session.room.main_consumer is not None:+ if main and session.room.main_consumer is not None:# The connection lock serializes attachment per key, so a# second main consumer means a caller bypassed it. The session# is in use either way, so its startup bookkeeping is done.@@ -293,7 +301,7 @@ class SessionManager:self._repository.update_session_id_sync(previous_id, session_id)- session.connect_consumer(session_consumer, main=True)+ session.connect_consumer(session_consumer, main=main)attached = Truereturn sessionfinally:diff --git a/tests/_server/test_session_manager.py b/tests/_server/test_session_manager.pyindex 439eec43a3..57c81f6994 100644--- a/tests/_server/test_session_manager.py+++ b/tests/_server/test_session_manager.py@@ -106,6 +106,7 @@ async def test_create_session_new(query_params={},file_key=NEW_FILE,auto_instantiate=False,+ main=True,)assert session_id in session_manager.sessionsassert session_manager.get_session(session_id) is session@@ -124,6 +125,7 @@ async def test_create_session_absolute_url(query_params={},file_key=temp_marimo_file,auto_instantiate=False,+ main=True,)assert session_id in session_manager.sessionsassert session_manager.get_session(session_id) is session@@ -264,6 +266,7 @@ async def test_create_session_with_script_config_overrides(query_params={},file_key=str(tmp_path / "test.py"),auto_instantiate=False,+ main=True,)assert session_id in session_manager.sessionsassert session_manager.get_session(session_id) is session@@ -471,6 +474,7 @@ async def test_recents_touch_called_on_session_create(query_params={},file_key=str(tmp_file),auto_instantiate=False,+ main=True,)# Allow async event to process@@ -509,7 +513,7 @@ async def test_concurrent_startup_survives_disconnected_waiter(async def connect() -> Session:return await session_manager.create_session(- session_id, mock_session_consumer, {}, NEW_FILE, False+ session_id, mock_session_consumer, {}, NEW_FILE, False, main=True)first = asyncio.create_task(connect())@@ -558,7 +562,7 @@ async def test_shutdown_waits_for_pending_startup_cleanup(monkeypatch.setattr(SessionImpl, "create", create)startup = asyncio.create_task(session_manager.create_session(- session_id, mock_session_consumer, {}, NEW_FILE, False+ session_id, mock_session_consumer, {}, NEW_FILE, False, main=True))await asyncio.wait_for(entered.wait(), 5)@@ -569,7 +573,12 @@ async def test_shutdown_waits_for_pending_startup_cleanup(assert not session_manager.sessionswith pytest.raises(KernelStartupError, match="shut down"):await session_manager.create_session(- session_id, mock_session_consumer, {}, NEW_FILE, False+ session_id,+ mock_session_consumer,+ {},+ NEW_FILE,+ False,+ main=True,)finally:release.set()@@ -818,6 +827,7 @@ async def test_abandoned_completed_startup_expires({},NEW_FILE,False,+ main=True,))try:@@ -860,6 +870,7 @@ async def test_startup_retention_ignores_long_session_ttl({},NEW_FILE,False,+ main=True,))try:@@ -901,6 +912,7 @@ async def test_startup_never_hands_out_a_session_owned_by_another_consumer({},NEW_FILE,False,+ main=True,)assert mock_session.room.main_consumer is otherassert not session_manager.is_session_starting(session_id, NEW_FILE)@@ -908,6 +920,71 @@ async def test_startup_never_hands_out_a_session_owned_by_another_consumer(await session_manager.shutdown()+async def test_secondary_consumer_joins_beside_the_main_one(+ session_manager: SessionManager,+ mock_session: Session,+ mock_session_consumer: SessionConsumer,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ from marimo._session.session import SessionImpl++ mock_session.app_file_manager = AppFileManager(filename=None)+ other = Mock(spec=SessionConsumer)+ other.consumer_id = "other_consumer_id"+ mock_session.room.add_consumer(other, main=True)++ async def create(**_kwargs: object) -> Session:+ return mock_session++ monkeypatch.setattr(SessionImpl, "create", create)+ try:+ session = await session_manager.create_session(+ session_id,+ mock_session_consumer,+ {},+ NEW_FILE,+ False,+ main=False,+ )+ assert session is mock_session+ assert mock_session.room.main_consumer is other+ assert mock_session.room.size == 2+ assert not session_manager.is_session_starting(session_id, NEW_FILE)+ finally:+ await session_manager.shutdown()+++async def test_secondary_consumer_leaves_the_main_seat_open(+ session_manager: SessionManager,+ mock_session: Session,+ mock_session_consumer: SessionConsumer,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ from marimo._session.session import SessionImpl++ mock_session.app_file_manager = AppFileManager(filename=None)++ async def create(**_kwargs: object) -> Session:+ return mock_session++ monkeypatch.setattr(SessionImpl, "create", create)+ try:+ await session_manager.create_session(+ session_id,+ mock_session_consumer,+ {},+ NEW_FILE,+ False,+ main=False,+ )+ assert mock_session.room.main_consumer is None+ assert mock_session.room.size == 1+ # A secondary consumer completes startup, preventing expiration.+ assert not session_manager.is_session_starting(session_id, NEW_FILE)+ finally:+ await session_manager.shutdown()++async def test_retry_waits_for_expired_startup_cleanup(session_manager: SessionManager,mock_session: Session,@@ -941,6 +1018,7 @@ async def test_retry_waits_for_expired_startup_cleanup({},NEW_FILE,False,+ main=True,))try:@@ -956,6 +1034,7 @@ async def test_retry_waits_for_expired_startup_cleanup({},NEW_FILE,False,+ main=True,))await asyncio.sleep(0)@@ -990,7 +1069,7 @@ async def test_closed_startup_cannot_be_attached(monkeypatch.setattr(SessionImpl, "create", create)first = asyncio.create_task(session_manager.create_session(- session_id, mock_session_consumer, {}, NEW_FILE, False+ session_id, mock_session_consumer, {}, NEW_FILE, False, main=True))try:@@ -1032,7 +1111,12 @@ async def test_closed_startup_cannot_be_attached(monkeypatch.setattr(SessionImpl, "create", restart)assert (await session_manager.create_session(- session_id, mock_session_consumer, {}, NEW_FILE, False+ session_id,+ mock_session_consumer,+ {},+ NEW_FILE,+ False,+ main=True,)is replacement)diff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.pyindex 43809178af..7aa8cedded 100644--- a/tests/_server/test_sessions.py+++ b/tests/_server/test_sessions.py@@ -626,6 +626,7 @@ def __():query_params={},file_key=file_key,auto_instantiate=False,+ main=True,)# Wait a loop to ensure the session is created@@ -663,6 +664,7 @@ def __():query_params={},file_key=file_key,auto_instantiate=False,+ main=True,)# Modify the file again@@ -840,6 +842,7 @@ async def test_watch_mode_with_watcher_on_save_autorun(tmp_path: Path) -> None:query_params={},file_key=str(tmp_file),auto_instantiate=False,+ main=True,)mock_session_view = MagicMock(spec=SessionView)session.session_view = mock_session_view@@ -949,6 +952,7 @@ async def test_watch_mode_with_watcher_on_save_lazy(tmp_path: Path) -> None:query_params={},file_key=str(tmp_file),auto_instantiate=False,+ main=True,)# Wait a bit for session to be ready@@ -1045,6 +1049,7 @@ def __():query_params={},file_key=str(tmp_path1),auto_instantiate=False,+ main=True,)# Try to rename to a non-existent file