Repository navigation
fix: 旧版が残した孤児 subscription を登録時に掃除する (capsicum#949) - #30
Conversation
device_id 導入前 (capsicum#932 以前) のクライアントは push トークンが変わるたびに
新しい行を作っていた。その行は上流 (Mastodon / Misskey) の購読が生きている限り
push され続けるため、1 通の通知が行数ぶん増える。
2026-08-10 に実機で確認した。pooza@misskey.delmulin.com の windows 行は 3 本
(7/13・7/22・8/6 作成) で、いずれも同じ PC の別々の WNS チャンネル URI を指して
おり、1 通のメンションに対し relay が 3 回受信・3 回送信していた。古い 2 行を
手で消したところ受信 1・送信 1 に戻り、体感も 3 通から 1 通になった。
capsicum#932 / #937 は「これから孤児を作らない」仕組みで、既に溜まった行には
効かない。実測でこの条件に当てはまる行が 75 あった (windows 39 / android 28 /
ios 8)。
登録のたびに、同じ (account, server, device_type) の古い行を消す。消すのは
3 つ揃ったときだけ:
1. device_id が無い (= 旧版が作った行)
2. 同じ組に device_id 付きの行が既にある (= そのインストールは更新済み)
3. LEGACY_ROW_GRACE_DAYS (14 日) 以上更新されていない
3 が要るのは、同じ OS の別実機がまだ旧版で動いている場合を巻き込まないため。
生きている端末は起動のたびに register して updated_at が進むので、猶予を超える
のは実際に使われていない行だけになる。仮に巻き込んでも次の起動で再登録されるので
自己修復する。
上流の購読は消せないが、行が無くなれば /push/{push_token} が 410 Gone を返し、
上流がその購読を破棄する。
クライアントは起動ごとに register するので専用のスイープ機構は置かない。使われて
いるインストールから順に自然に掃除されていく。既存の 75 行もこれで消える。
検証: test/legacy_row_purge_test.rb に 8 件。巻き込み防止 (猶予内 / 別端末種別 /
別アカウント / device_id 付き / 旧クライアントの登録では走らない / 自分自身)
を厚く固定した。relay 全 24 件緑、RuboCop clean。
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d49390c53e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| "Purging legacy subscription id=#{row['id']} (#{account}/#{device_type}," \ | ||
| " last seen #{row['updated_at']})", | ||
| ) | ||
| @db.execute('DELETE FROM subscriptions WHERE id = ?', [row['id']]) |
There was a problem hiding this comment.
Recheck staleness before deleting each row
With the configured multi-threaded Puma server, a legacy client can refresh one of these rows after the SELECT but before this unconditional DELETE: one /register request selects the stale ID, another request upserts the same token and advances updated_at, and the first request then deletes the now-active registration. Include the stale and device_id IS NULL predicates in the delete itself, or serialize the selection and deletion, so a concurrent refresh cannot cause notification loss.
Useful? React with 👍 / 👎.
capsicum#949 の恒久対処。main への直接 push が保護ルールで弾かれるため PR にしています。
実機で確認した現象
pooza@misskey.delmulin.comの windows 行が 3 本(7/13・7/22・8/6 作成)あり、いずれも同じ PC の別々の WNS チャンネル URIを指していました。1 通のメンションに対し relay のログは受信 3・送信 3 で、体感も 3 通。古い 2 行を手で消したら受信 1・送信 1 に戻り、1 通になりました。capsicum#932 / #937 は「これから孤児を作らない」仕組みで、既に溜まった行には効きません。実測でこの条件に当てはまる行が 75 ありました(windows 39 / android 28 / ios 8)。
やること
登録のたびに、同じ
(account, server, device_type)の古い行を消します。消すのは 3 つ揃ったときだけです。device_idが無い(=旧版が作った行)device_id付きの行が既にある(=そのインストールは更新済み)LEGACY_ROW_GRACE_DAYS(14 日)以上更新されていない3 が要るのは、同じ OS の別実機がまだ旧版で動いている場合を巻き込まないためです。生きている端末は起動のたびに register して
updated_atが進むので、猶予を超えるのは実際に使われていない行だけになります。仮に巻き込んでも次の起動で再登録されるので自己修復します。上流の購読は消せませんが、行が無くなれば
/push/{push_token}が 410 Gone を返し、上流がその購読を破棄します。**専用のスイープ機構は置きません。**クライアントは起動ごとに register するので、使われているインストールから順に自然に掃除されます。既存の 75 行もこれで消えます。
検証
test/legacy_row_purge_test.rbに 8 件。巻き込み防止(猶予内 / 別端末種別 / 別アカウント /device_id付き / 旧クライアントの登録では走らない / 自分自身)を厚く固定しました。relay 全 24 件緑・RuboCop clean。🤖 Generated with Claude Code