Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/qwenpaw/app/runner/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,45 @@
logger = logging.getLogger(__name__)


def _safe_json_loads(content: str, filepath: str = "") -> dict:
"""Parse JSON with corruption recovery.

Attempts standard ``json.loads`` first. If that fails due to
trailing garbage (a common symptom of concurrent-write race
conditions), falls back to ``raw_decode`` to extract the first
valid JSON object. If the file is completely unparseable, returns
an empty dict and logs a warning so callers never crash.

Args:
content: Raw file content.
filepath: Used only for log messages.

Returns:
Parsed dict, or ``{}`` when the content is beyond recovery.
"""
try:
return json.loads(content)
except json.JSONDecodeError:
pass

# Try to extract the first valid JSON object.
try:
result, _ = json.JSONDecoder().raw_decode(content)
logger.warning(
"Session file %s had corrupted JSON. "
"Recovered first valid object via raw_decode.",
filepath,
)
return result
except json.JSONDecodeError:
logger.warning(
"Session file %s is completely corrupted and could not "
"be recovered. Returning empty dict.",
filepath,
)
return {}


# Characters forbidden in Windows filenames
_UNSAFE_FILENAME_RE = re.compile(r'[\\/:*?"<>|]')

Expand Down Expand Up @@ -111,7 +150,7 @@ async def load_session_state(
errors="surrogatepass",
) as f:
content = await f.read()
states = json.loads(content)
states = _safe_json_loads(content, session_save_path)

for name, state_module in state_modules_mapping.items():
if name in states:
Expand Down Expand Up @@ -154,7 +193,7 @@ async def update_session_state(
errors="surrogatepass",
) as f:
content = await f.read()
states = json.loads(content)
states = _safe_json_loads(content, session_save_path)

else:
if not create_if_not_exist:
Expand Down Expand Up @@ -223,7 +262,7 @@ async def get_session_state_dict(
errors="surrogatepass",
) as file:
content = await file.read()
states = json.loads(content)
states = _safe_json_loads(content, session_save_path)

logger.info(
"Get session state dict from %s successfully.",
Expand Down
Loading
Loading