Skip to content

fix: --fresh fails to clean cookie_storage, auth_svc crashes on key mismatch#3355

Merged
uruwhy merged 5 commits intomasterfrom
fix/fresh-cookie-cleanup
Apr 14, 2026
Merged

fix: --fresh fails to clean cookie_storage, auth_svc crashes on key mismatch#3355
uruwhy merged 5 commits intomasterfrom
fix/fresh-cookie-cleanup

Conversation

@deacon-mp
Copy link
Copy Markdown
Contributor

Summary

  • Add data/cookie_storage to DATA_FILE_GLOBS so --fresh properly removes it
  • Handle decryption failure in auth_svc gracefully: catch SystemExit from file_svc._read(), regenerate session key instead of crashing

Problem

After PR #3264 (persistent sessions), data/cookie_storage is saved as an encrypted file. When the encryption key changes (e.g., switching between --insecure and secure mode), --fresh fails to clean it because it's not in the managed file list. On next startup, file_svc._read() hits InvalidToken and calls sys.exit(1), crashing the server even with --fresh.

Reproduction

python3 server.py --insecure -l DEBUG   # creates cookie_storage with default.yml key
# Ctrl+C
python3 server.py -l DEBUG              # uses local.yml key → CRASH
python3 server.py --fresh -l DEBUG      # cookie_storage survives → CRASH again

Changes

File Change
app/service/data_svc.py Add data/cookie_storage to DATA_FILE_GLOBS
app/service/auth_svc.py Catch SystemExit from _read(), delete stale cookie, regenerate key
tests/services/test_fresh_cleanup.py 3 tests: glob membership + stale cookie recovery with state save/restore

Test plan

  • Reproduced crash on master, confirmed fix on this branch
  • --fresh now removes data/cookie_storage
  • Key mismatch no longer crashes — auth_svc regenerates and continues
  • Test saves/restores BaseWorld._app_configuration — no state leakage
  • 3 tests pass, flake8 clean

🤖 Generated with Claude Code

…ismatch

- Add data/cookie_storage to DATA_FILE_GLOBS so --fresh removes it
- Catch SystemExit in auth_svc when file_svc._read() fails to decrypt
  stale cookie_storage; regenerate session key instead of crashing
- Add tests: DATA_FILE_GLOBS membership + stale cookie recovery

Fixes crash when switching between --insecure and secure mode after
PR #3264 introduced persistent session cookies.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes --fresh cleanup and startup robustness for persistent sessions by ensuring data/cookie_storage is treated as managed state and by preventing auth_svc from crashing when the encryption key used to decrypt cookie_storage changes.

Changes:

  • Add data/cookie_storage to DATA_FILE_GLOBS so DataService.destroy() (used by --fresh) removes it.
  • Update AuthService.apply() to recover from cookie_storage decryption failure by deleting the stale file and regenerating a session key instead of terminating the server.
  • Add tests validating glob membership and key-mismatch recovery behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
app/service/data_svc.py Includes data/cookie_storage in the --fresh managed deletion globs.
app/service/auth_svc.py Catches SystemExit from file_svc._read() on key mismatch and regenerates cookie_storage.
tests/services/test_fresh_cleanup.py Adds regression tests for DATA_FILE_GLOBS membership and stale-cookie recovery.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@RachHavoc
Copy link
Copy Markdown
Contributor

Validated this fix resolves the problem of cookie storage not cleaning up with --fresh let's get this mergedddd @deacon-mp

@fionamccrae
Copy link
Copy Markdown
Contributor

fionamccrae commented Apr 8, 2026

Verified clean exit when not using --fresh and switching between regular/insecure deployment, in addition to update of cookie storage when --fresh is used

Copy link
Copy Markdown
Contributor

@uruwhy uruwhy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see requested changes in above comments

@sonarqubecloud
Copy link
Copy Markdown

❌ The last analysis has failed.

See analysis details on SonarQube Cloud

…up since only necessary change to fix issue is adding data/cookie_storage to DATA_FILE_GLOBS
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@sonarqubecloud
Copy link
Copy Markdown

❌ The last analysis has failed.

See analysis details on SonarQube Cloud

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@uruwhy
Copy link
Copy Markdown
Contributor

uruwhy commented Apr 14, 2026

reverted changes except adding the cookie storage file to the list of files deleted on --fresh. There was no need to handle the sys.exit exception to begin with, since --fresh will delete files before app_svc.apply even kicks off. So if --fresh is toggled, auth_svc.apply will always create a new cookie file and will not run into the InvalidToken issue from decryption.

Verified that switching between different config files (with diff encryption keys) worked fine with --fresh and that --fresh deletes the existing cookie storage file

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@uruwhy
Copy link
Copy Markdown
Contributor

uruwhy commented Apr 14, 2026

sonarcloud complaint is about a section of code that's not actually part of this PR, so bypassing for now

@uruwhy uruwhy merged commit f630405 into master Apr 14, 2026
14 of 15 checks passed
@uruwhy uruwhy deleted the fix/fresh-cookie-cleanup branch April 14, 2026 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants