Skip to content

Commit

Permalink
fix: safer check before removing session (#3838)
Browse files Browse the repository at this point in the history
With multiple clients. It's possible they try to remove each other at
the same time from different callsites, so this adds an extra check
  • Loading branch information
mscolnick authored Feb 18, 2025
1 parent 071e57b commit 70a6d64
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
6 changes: 5 additions & 1 deletion marimo/_ast/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ast
import base64
import inspect
import sys
import threading
from dataclasses import asdict, dataclass, field
from pathlib import Path
Expand All @@ -25,7 +26,10 @@
)
from uuid import uuid4

from typing_extensions import ParamSpec, TypeAlias
if sys.version_info < (3, 10):
from typing_extensions import ParamSpec, TypeAlias
else:
from typing import ParamSpec, TypeAlias

from marimo import _loggers
from marimo._ast.cell import Cell, CellConfig, CellId_t, CellImpl
Expand Down
6 changes: 5 additions & 1 deletion marimo/_ast/cell_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import random
import string
import sys
from typing import (
TYPE_CHECKING,
Callable,
Expand All @@ -13,7 +14,10 @@
TypeVar,
)

from typing_extensions import ParamSpec, TypeAlias
if sys.version_info < (3, 10):
from typing_extensions import ParamSpec, TypeAlias
else:
from typing import ParamSpec, TypeAlias

from marimo._ast.cell import Cell, CellConfig, CellId_t
from marimo._ast.compiler import cell_factory, toplevel_cell_factory
Expand Down
5 changes: 3 additions & 2 deletions marimo/_server/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ def maybe_resume_session(
# Set new session and remove old session
self.sessions[new_session_id] = session
# If the ID is the same, we don't need to delete the old session
if new_session_id != session_id:
if new_session_id != session_id and session_id in self.sessions:
del self.sessions[session_id]
return session

Expand Down Expand Up @@ -1088,7 +1088,8 @@ def close_session(self, session_id: SessionId) -> bool:
)

session.close()
del self.sessions[session_id]
if session_id in self.sessions:
del self.sessions[session_id]
return True

def close_all_sessions(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions tests/snapshots/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ hstack
icon
iframe
image
import_guard
islands
MarimoIslandGenerator
MarimoIslandStub
Expand Down

0 comments on commit 70a6d64

Please sign in to comment.