D4Vinci/Scrapling · error · KeyError
Session '{session_id}' not found
Error message
Session '{session_id}' not found What it means
SessionManager.pop()/remove() raise KeyError when asked for a session_id that is not registered. The manager keeps sessions in a dict keyed by id; removing an unknown id means it was never added, was already removed, or the id is misspelled.
Source
Thrown at scrapling/spiders/session.py:56
if lazy:
self._lazy_sessions.add(session_id)
return self
def remove(self, session_id: str) -> None:
"""Removes a session.
:param session_id: ID of session to remove
"""
_ = self.pop(session_id)
def pop(self, session_id: str) -> Session:
"""Remove and returns a session.
:param session_id: ID of session to remove
"""
if session_id not in self._sessions:
raise KeyError(f"Session '{session_id}' not found")
session = self._sessions.pop(session_id)
if session_id in self._lazy_sessions:
self._lazy_sessions.remove(session_id)
if session and self._default_session_id == session_id:
self._default_session_id = next(iter(self._sessions), None)
return session
@property
def default_session_id(self) -> str:
if self._default_session_id is None:
raise RuntimeError("No sessions registered")
return self._default_session_id
@property
def session_ids(self) -> list[str]:View on GitHub (pinned to 5d213a2d47)
Solutions
- Guard removals: if session_id in manager.session_ids: manager.remove(session_id).
- Use session_ids (or a try/except KeyError) to make teardown idempotent.
- Centralize session-id constants in one place instead of repeating string literals.
Example fix
# before
manager.remove('htp') # KeyError (typo)
# after
if 'http' in manager.session_ids:
manager.remove('http') Defensive patterns
Strategy: validation
Validate before calling
def safe_remove(manager, session_id: str) -> bool:
if session_id in manager.session_ids:
manager.remove(session_id)
return True
return False Try / catch
from contextlib import suppress
with suppress(KeyError):
manager.remove(session_id) # idempotent teardown Prevention
- Make teardown idempotent — guard removes with session_ids membership or suppress KeyError.
- Centralize session-id literals to avoid typo-driven misses.
When it happens
Trigger: manager.remove('http') when only 'browser' was registered; double-cleanup code that removes the same session in teardown and again in an outer finally block.
Common situations: Teardown routines running twice (finally + atexit); typos in session ids; removing a session that a failed setup step never actually registered.
Related errors
- Session '{session_id}' not found. Available: {available}
- {self.__class__.__name__}.configure_sessions() did not add a
- Session '{session_id}' not found. Use list_sessions to see a
- Session '{session_id}' is no longer alive. Open a new sessio
- Session '{session_id}' is a '{entry.session_type}' session,
AI-assisted analysis of D4Vinci/Scrapling@5d213a2d47 (2026-08-14).
Data as JSON: /api/errors/92d0f631513887c8.
Report an issue: GitHub.