Repository navigation
Description
Environment
- Octop (TencentCloud/Octop)
1.0.2b9, commit42e41ed33e6b5f43fda3cc685dff94ce0e2aec3a - Python 3.12.16, Linux x86_64
octop-browser1.0.0
Summary
_profile_port() in src/octop/infra/browser/setup.py wraps the profile-registry lookup in a bare except Exception: return None. Its only caller treats None as "this profile has no CDP port, so no browser is running" and runs the destructive branch: it kills the Chrome process for that profile and clears the profile locks.
If the profile registry cannot be decoded (for example a UTF-16/BOM leftover or a partially overwritten JSON file), the lookup raises UnicodeDecodeError; the exception is swallowed, the "is CDP already listening?" guard is skipped, and a browser that is actually alive is killed. The preparation function still returns success, and the same corrupt registry later surfaces as an unrelated 503 when the dashboard tries to attach to the profile.
Root cause
def _profile_port(profile_name: str) -> int | None:
try:
from octop_browser.profile import ProfileManager
pm = ProfileManager()
profile = pm.get_or_create(profile_name)
return int(profile.cdp_port)
except Exception:
return NoneNone conflates two different answers:
- the profile has no assigned port / Chrome is not listening — safe to treat as not running;
- the lookup itself failed — the caller must not run the destructive teardown.
The consumer relies on that distinction:
port = await asyncio.to_thread(_profile_port, profile_name)
if port is not None and await _cdp_port_listening(port):
return data_dir # live browser: leave it alone
killed = await asyncio.to_thread(pkill_chrome_profile, data_dir)
cleared = await asyncio.to_thread(clear_profile_locks, data_dir)The docstring of the preparation function promises "If CDP is already listening, leaves the running browser alone". With a corrupt registry the port is unknown, the guard is skipped, and the running browser is torn down.
Affected code
src/octop/infra/browser/setup.py—_profile_port()(line 437) andprepare_harness_profile_for_launch()(guard at lines 302-304, teardown after it)src/octop/api/routers/browser/harness.py—resolve_harness_session()calls the preparation function and later reports the decode error as503
Reproduction
- Create a profile registry (
.octop-browser-profiles.json) that decodes normally and a profileuser-1with CDP port9222. - Start a real local CDP endpoint that answers
GET /json/versionwith200, and a real child process whose argv contains--user-data-dir=<profile dir>(the string the product'spkill_chrome_profile()matches). - Run the real profile-preparation path.
Healthy registry:
_profile_port('user-1') -> 9222
prepare -> .../browser-profiles/user-1
chrome pid=4664 alive=True # CDP reused, early return
Corrupt registry (contains bytes b'\xff\xfe{"user-1": 9222}\x00\x81'):
_profile_port('user-1') -> None
prepare -> .../browser-profiles/user-1 # same success return
chrome pid=4664 alive=False # killed by the product's pkill
Then the product attach path:
resolve_harness_session()
-> OctopError: failed to attach browser profile 'user-1':
'utf-8' codec can't decode byte 0xff in position 0: invalid start byte (status 503)
The kill is performed by pkill_chrome_profile(data_dir) after the skipped guard; the decode error itself is never logged by _profile_port.
Expected behavior
A profile-registry read failure must not be interpreted as "no browser is running". The function should either surface the decode error (and leave the live browser untouched), or narrow the caught exception to the "profile does not exist" case and re-raise read/parse failures. The caller must not run pkill_chrome_profile / clear_profile_locks when the port lookup failed.
Actual behavior
UnicodeDecodeError is swallowed, _profile_port returns None, the CDP-listening guard is skipped, the live browser process is killed, and the preparation function still reports success. The attach request then fails with 503 and an unrelated decode message, after the browser has already been destroyed. A profile registry that is unreadable but writable can also be silently overwritten.
Suggested fix
Catch only the expected "profile not found" exception and let UnicodeDecodeError, json.JSONDecodeError and OSError from reading the registry propagate (or return a distinct error value). In the preparation function, skip the kill/clear branch when the lookup failed, and log the underlying decode error so the subsequent 503 points at the registry rather than at the attach step.
Activity