Skip to content

fix: 復元後マジックコマンドのみ実行→shutdown時のKeyErrorを修正 (#26) - #43

Merged
MRyutaro merged 1 commit into
mainfrom
fix/issue-26-checkpoint-keyerror
Jun 14, 2026
Merged

MRyutaro merged 1 commit into
mainfrom
fix/issue-26-checkpoint-keyerror

Conversation

@MRyutaro

Copy link
Copy Markdown
Owner

概要

Issue #26 のチェックポイント保存時 KeyError を修正します。

症状

  1. カーネル再起動でチェックポイントから復元
  2. 通常のセルを実行せず %whos などマジックコマンドのみ実行
  3. カーネル shutdown

このとき保存処理で KeyError(例: KeyError: 'datetime')が発生していた。アクティブ変数が2個以上のとき再現する(1個だと checkpoint.py の二重ループで active_vs1 != active_vs2 が常に偽になり、該当アクセスが評価されないため出ない)。

原因

復元直後は fingerprint_dict(変数の指紋)が空。マジックコマンドは record_event をスキップするため空のまま埋まらず、その状態で checkpoint() が fingerprint_dict[変数名] に存在チェックなしでアクセスして落ちる。

修正

  • elastic_notebook.py load_checkpoint(根本対応 / 解決方法2): 復元した変数の fingerprint を再構築しておき、以降の checkpoint() / record_event() が空 dict を踏まないようにする。
  • checkpoint.py overlapping 判定(防御 / 解決方法1): fingerprint_dict の存在チェックを追加し KeyError を回避。
  • checkpoint.py サイズ計測ループ(防御): user_ns に存在しないアクティブ変数はサイズ∞(マイグレート対象外)扱いでスキップ。

検証

  • 修正前: 2変数シナリオ(datetime + 他)で Error saving checkpoint: 'datetime' を Docker(公開イメージ相当)で再現。
  • 修正後: 同シナリオで KeyError が消えることを確認。

補足(別件)

別途報告のある「再起動後 Run All がセル1で止まる/2回目で通る」現象は、本修正の対象外です。あちらは JupyterLab の再起動時 iopub(PUB/SUB) 再接続レースで、素の ipykernel でも発生するため本 PR では扱いません。

Closes #26

🤖 Generated with Claude Code

チェックポイント復元直後は fingerprint_dict が空になる。マジックコマンド
(%whos 等)は record_event をスキップするため空のまま埋まらず、その状態で
checkpoint() を実行すると fingerprint_dict[変数名] への存在チェックなしの
アクセスで KeyError が発生していた(アクティブ変数が2個以上で再現)。

- load_checkpoint: 復元した変数の fingerprint を再構築しておく(根本対応)
- checkpoint(): overlapping 判定で fingerprint_dict の存在チェックを追加
- checkpoint(): user_ns に無いアクティブ変数はサイズ∞扱いでスキップ(防御)

Closes #26

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@MRyutaro
MRyutaro merged commit 923d8e0 into main Jun 14, 2026
1 check passed
@MRyutaro
MRyutaro deleted the fix/issue-26-checkpoint-keyerror branch June 14, 2026 15:12
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.

チェックポイント時のバグの解消

1 participant