fix(controller): propagate close-state to all shared-controller holders#24
Open
Nick Hehr (HipsterBrown) wants to merge 1 commit into
Open
fix(controller): propagate close-state to all shared-controller holders#24Nick Hehr (HipsterBrown) wants to merge 1 commit into
Nick Hehr (HipsterBrown) wants to merge 1 commit into
Conversation
The ref-counted controller registry handed each consumer (arm + gripper on one SO-101 share a serial port) its own *SafeSoArmController struct copy wrapping the same bus. When one component closed first, ReleaseController closed the shared bus but the other holder's struct had no way to observe it, so its next call raced against / hit a closed bus. Fix, extracted from the lifecycle-correctness work in #11: - Add a `closed atomic.Bool` to SafeSoArmController plus an ErrControllerClosed sentinel; every public method checks it up-front and returns the sentinel instead of touching a closed bus. Close() is now idempotent (sets closed). - getExistingController / createNewController return the *cached* controller pointer instead of a fresh struct copy, so all holders share one closed flag. ReleaseController and ForceCloseController set closed=true before closing the bus, so surviving holders observe ErrControllerClosed atomically. checkClosed is best-effort (documented): callers must hold a registry refcount for the duration of a call. The runtime.Caller-based release tracking is left intact here; replacing it was a separate change in #11 and is out of scope. Tests: add a scripted mock-bus harness (no hardware) and cover the gated-method sentinel across all 9 methods, same-pointer-per-port, and release-closes-all- consumers. Full package passes under -race. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JNvy6dGUT6wBHLNBv8YPYv
This was referenced Jul 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The arm and gripper on one SO-101 share a serial port via the ref-counted controller registry. Previously each consumer got its own
*SafeSoArmControllerstruct copy wrapping the same bus. When one component closed first,ReleaseControllerclosed the shared bus, but the other holder's struct had no way to observe it — its next call raced against / hit a closed bus.This is the data-race fix extracted from #11 (which bundled several unrelated refactors). Only the race fix is here; the rest is intentionally left out (see below).
Changes
manager.go— add aclosed atomic.BooltoSafeSoArmControllerplus anErrControllerClosedsentinel. Every public method checks it up-front and returns the sentinel instead of touching a closed bus.Close()is now idempotent (setsclosed).registry.go—getExistingController/createNewControllerreturn the cached controller pointer instead of a fresh per-caller struct copy, so all holders share oneclosedflag.ReleaseControllerandForceCloseControllersetclosed=truebefore closing the bus, so surviving holders observeErrControllerClosedatomically on their next call.mock_bus_test.go, no hardware) + regression tests (lifecycle_test.go): the gated-method sentinel across all 9 methods, same-pointer-per-port, and release-closes-all-consumers.checkClosedis best-effort (documented in-code): callers must hold a registry refcount for the duration of a call. Theruntime.Caller-based release tracking is left intact here, so the fix is self-contained.Explicitly out of scope (deferred from #11)
runtime.Callerrelease tracking with explicit port handlesctxthreading through init/diagnose/verifymotion.FromProvidercontroller-leak fix andcancelCtx/cancelFunccleanupTesting
gofmt -sclean,go vet ./cmd/module/ .cleango test -race ./cmd/module/ .— fullso_armpackage passes under the race detectorNote
The cached-pointer approach means a second component reacquiring a port with a different calibration file updates the shared
calibratedServosbut not the controller struct'scalibrationfield. This is pre-existing behavior carried over from #11's design, unrelated to the race, and only affects the rare reacquire-with-different-calibration path — flagged for awareness.🤖 Generated with Claude Code
https://claude.ai/code/session_01JNvy6dGUT6wBHLNBv8YPYv