state: GetSnapshot copies the state's maps (closes #261) #272

Merged
clawbot merged 1 commits from fix/261-snapshot-map-copies into next 2026-10-13 13:44:32 +02:00
Collaborator

Fixes #261.

GetSnapshot copied the snapshot struct but returned the state's own four maps. The dashboard and /api/v1/status range over them after the read lock is released while the watcher writes them, which is a data race and can abort the process with concurrent map iteration and map write. GetSnapshot now copies Domains, Hostnames, Ports and Certificates under the read lock. Of the two fixes the issue allows, this is the smaller: the handlers are unchanged.

The entries in the maps are still shared with the state. The issue's claim holds: the watcher changes an entry only by saving a new one with a Set...State call, and no caller of GetSnapshot or of the Get...State getters changes an entry it got back (Load sorts records in place, but in a snapshot that is not yet the state's). The comment on GetSnapshot states the rule for whoever adds a writer.

TestGetSnapshot_WhileWritten marshals snapshots to JSON, which ranges over every map, while a goroutine writes all four maps; -race reports a race on each map when the copies are removed. With the copies removed, the race detector may also name another parallel state test as failed, since it blames whichever test is running. TestGetSnapshot_ReturnsCopy now adds an entry to each map of a snapshot and checks that the state does not have it.

Model: opus-5-5

Fixes https://git.eeqj.de/sneak/dnswatcher/issues/261. `GetSnapshot` copied the snapshot struct but returned the state's own four maps. The dashboard and `/api/v1/status` range over them after the read lock is released while the watcher writes them, which is a data race and can abort the process with `concurrent map iteration and map write`. `GetSnapshot` now copies `Domains`, `Hostnames`, `Ports` and `Certificates` under the read lock. Of the two fixes the issue allows, this is the smaller: the handlers are unchanged. The entries in the maps are still shared with the state. The issue's claim holds: the watcher changes an entry only by saving a new one with a `Set...State` call, and no caller of `GetSnapshot` or of the `Get...State` getters changes an entry it got back (`Load` sorts records in place, but in a snapshot that is not yet the state's). The comment on `GetSnapshot` states the rule for whoever adds a writer. `TestGetSnapshot_WhileWritten` marshals snapshots to JSON, which ranges over every map, while a goroutine writes all four maps; `-race` reports a race on each map when the copies are removed. With the copies removed, the race detector may also name another parallel `state` test as failed, since it blames whichever test is running. `TestGetSnapshot_ReturnsCopy` now adds an entry to each map of a snapshot and checks that the state does not have it. Model: opus-5-5
clawbot added the needs-review label 2026-10-13 13:17:15 +02:00
clawbot self-assigned this 2026-10-13 13:17:15 +02:00
clawbot added 1 commit 2026-10-13 13:17:15 +02:00
The dashboard and /api/v1/status range over the maps in the snapshot
GetSnapshot returns after the read lock is released, while the watcher
writes the same maps under the lock. Go aborts the process when a write
lands during such a range. GetSnapshot now returns copies of the four
maps.

The entries in the maps are still shared with the state. That is safe
because the watcher replaces an entry with a Set call and never changes
one in place; the comment on GetSnapshot says so.

TestGetSnapshot_WhileWritten ranges over snapshots while another
goroutine writes the state and fails under -race without the copies.
TestGetSnapshot_ReturnsCopy now checks that each map is not the state's
own.

Model: opus-5-5
Author
Collaborator

Review: PASS

Model: opus-5-5

Review: PASS Model: opus-5-5
clawbot removed the needs-review label 2026-10-13 13:34:25 +02:00
clawbot merged commit 02cb1ec580 into next 2026-10-13 13:44:32 +02:00
clawbot deleted branch fix/261-snapshot-map-copies 2026-10-13 13:44:32 +02:00
Sign in to join this conversation.