{"record":{"id":"a3fdcf161405bf18","repo":"langchain-ai/deepagents","slug":"unixsocketeventsource-is-already-started","errorCode":null,"errorMessage":"UnixSocketEventSource is already started","messagePattern":"UnixSocketEventSource is already started","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"libs/code/deepagents_code/event_bus.py","lineNumber":157,"sourceCode":"        self,\n        sink: Callable[[ExternalEvent], Awaitable[None]],\n    ) -> None:\n        \"\"\"Start listening for newline-delimited JSON events.\n\n        Args:\n            sink: Async callback invoked with each decoded event.\n\n        Raises:\n            RuntimeError: If `start()` has already been called on this\n                instance without a subsequent `stop()`.\n            FileExistsError: If the socket path is occupied by a non-socket\n                filesystem entry.\n            OSError: If the socket cannot be bound (e.g. permission denied,\n                path too long).\n        \"\"\"  # noqa: DOC502  # FileExistsError/OSError propagate from helpers\n        if self._server is not None:\n            msg = \"UnixSocketEventSource is already started\"\n            raise RuntimeError(msg)\n\n        self._sink = sink\n        self.path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)\n        with contextlib.suppress(FileNotFoundError):\n            _unlink_existing_socket(self.path)\n\n        previous_umask = os.umask(0o077)\n        try:\n            self._server = await asyncio.start_unix_server(\n                self._handle_client,\n                path=str(self.path),\n                limit=_MAX_LINE_BYTES,\n            )\n        finally:\n            os.umask(previous_umask)\n\n        # Defense in depth: even if `start_unix_server` somehow ignored the\n        # umask (different libc, mocked socket layer), force-tighten the mode.","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/code/deepagents_code/event_bus.py#L139-L175","documentation":"`UnixSocketEventSource.start()` binds and serves on a Unix socket; calling it twice would attempt to rebind a socket the instance already owns. The method raises this RuntimeError when the internal server handle (`self._server`) is already set, enforcing a strict start-once lifecycle. `stop()` is the idempotent counterpart for cleanup.","triggerScenarios":"Calling `start()` a second time on the same instance — e.g. a reconnect routine that reuses the object, or an init path racing a background starter without checking state.","commonSituations":"Retry logic that restarts the same source instead of creating a new one; app reload hooks firing initialization twice; wiring that starts the source in both a screen handler and an app-level hook.","solutions":["Track a started flag yourself and skip `start()` when it is already set.","Call `await source.stop()` before a deliberate restart, then `start()` again.","Create a fresh `UnixSocketEventSource` instance for each start cycle instead of restarting the old one.","Move startup to a single place in the app lifecycle so it runs exactly once."],"exampleFix":"// before\nasync def reconnect(src):\n    await src.start()  # RuntimeError if already started\n// after\nasync def reconnect(src):\n    await src.stop()\n    await src.start()","handlingStrategy":"try-catch","validationCode":"if getattr(source, \"_server\", None) is not None:\n    return  # already started\nawait source.start()","typeGuard":"def is_started(source) -> bool:\n    return getattr(source, \"_server\", None) is not None","tryCatchPattern":"try:\n    await source.start()\nexcept RuntimeError as exc:\n    if \"already started\" in str(exc):\n        logger.debug(\"Event source already running; skipping start\")\n    else:\n        raise","preventionTips":["Track your own started flag around the source lifecycle.","Route all start/restart logic through one idempotent helper that stops before starting.","Avoid starting the same source from multiple app hooks."],"tags":["event-bus","lifecycle","unix-socket","double-start"],"backgroundTag":"already-started","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}