Repository navigation
fix: stop extensions from wedging osqueryd's extension manager; bump dot1x to #113 head #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Robbie Trencheny (robbiet480)
merged 4 commits into
main
from
dot1x-duplicate-registration
Oct 5, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
49deb5f
chore(dot1x): bump dot1x table to macadmins/osquery-extension#113 hea…
robbiet480 46bf27d
test(dot1x): reproduce the duplicate-registration wedge from host 671
robbiet480 46deda5
fix: keep extensions from wedging osqueryd's extension manager
robbiet480 ade04b4
fix(extserver): never register after shutdown; address review
robbiet480 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| name: test | ||
|
|
||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: [main] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: macos-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| persist-credentials: false | ||
| - uses: actions/setup-go@v5 | ||
| with: | ||
| go-version-file: extserver/go.mod | ||
| cache-dependency-path: "*/go.sum" | ||
| - name: vet and test | ||
| run: | | ||
| for m in extserver dot1x mac_enclosure_color; do | ||
| (cd "$m" && go vet ./... && go test -race ./...) | ||
| done | ||
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| // Package extserver runs a Campus osquery extension. It replaces osquery-go's | ||
| // ExtensionManagerServer.Run, whose failure handling wedged osqueryd's | ||
| // extension manager on several Macs (first seen on Fleet host 671, | ||
| // 2026-10-05): | ||
| // | ||
| // - Run exits on the first failed ping. "timeout after 200ms" is not | ||
| // osqueryd being gone: it is osquery-go's client lock wait (200ms by | ||
| // default), which can fail even uncontended after a scheduling stall. | ||
| // - The exited extension leaves its registration for osqueryd to reap, and | ||
| // osqueryd 5.23.1 can block reaping indefinitely behind a running query. | ||
| // Every respawn is then refused with "Duplicate extension registered" and | ||
| // exits, every ~3s, until osqueryd restarts. | ||
| // | ||
| // Run instead waits up to the connection timeout for the client lock, | ||
| // tolerates transient ping failures, retries duplicate registrations | ||
| // in-process with backoff, and always deregisters on the way out (including | ||
| // on SIGTERM/SIGINT). | ||
| package extserver | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "os/signal" | ||
| "strings" | ||
| "sync" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| osquery "github.com/osquery/osquery-go" | ||
| gen "github.com/osquery/osquery-go/gen/osquery" | ||
| ) | ||
|
|
||
| const ( | ||
| // maxPingFailures consecutive failed pings mean osquery is really gone. | ||
| maxPingFailures = 3 | ||
| // maxRetryBackoff caps the wait between duplicate-registration retries. | ||
| maxRetryBackoff = time.Minute | ||
| ) | ||
|
|
||
| // Main parses the standard osquery extension flags (--socket, --timeout, | ||
| // --interval, --verbose), registers plugins as extension name/version and | ||
| // serves until osquery goes away or the process is signalled. | ||
| func Main(name, version string, plugins ...osquery.OsqueryPlugin) { | ||
| socket := flag.String("socket", "", "Path to the osquery extension socket") | ||
| timeoutSec := flag.Int("timeout", 3, "Seconds to wait for a successful connection") | ||
| intervalSec := flag.Int("interval", 3, "Seconds between connection checks") | ||
| verbose := flag.Bool("verbose", false, "Enable verbose extension logging") | ||
| flag.Parse() | ||
| _ = *verbose | ||
|
|
||
| if *socket == "" { | ||
| log.Fatalln("--socket is required") | ||
| } | ||
| timeout := time.Duration(*timeoutSec) * time.Second | ||
| interval := time.Duration(*intervalSec) * time.Second | ||
|
|
||
| // Wait up to timeout for the client's socket lock instead of osquery-go's | ||
| // 200ms default. | ||
| client, err := osquery.NewClient(*socket, timeout, osquery.DefaultWaitTime(timeout)) | ||
| if err != nil { | ||
| log.Fatalf("error creating extension manager client: %s", err) | ||
| } | ||
|
|
||
| server, gated, err := newServer(name, version, *socket, client, timeout, plugins...) | ||
| if err != nil { | ||
| client.Close() | ||
| log.Fatalf("error creating extension manager: %s", err) | ||
| } | ||
|
|
||
| ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
| err = run(ctx, server, gated, interval) | ||
| stop() | ||
| client.Close() | ||
| if err != nil { | ||
| log.Fatalln(err) | ||
| } | ||
| } | ||
|
|
||
| // gate refuses registrations once shut, so none can begin after run starts | ||
| // shutting down. osquery-go only guards this for clients it creates itself. | ||
| type gate struct { | ||
| osquery.ExtensionManager | ||
| mu sync.Mutex | ||
| shut bool | ||
| } | ||
|
|
||
| func (g *gate) RegisterExtension(info *gen.InternalExtensionInfo, registry gen.ExtensionRegistry) (*gen.ExtensionStatus, error) { | ||
| g.mu.Lock() | ||
| defer g.mu.Unlock() | ||
| if g.shut { | ||
| return nil, errors.New("shutting down") | ||
| } | ||
| return g.ExtensionManager.RegisterExtension(info, registry) | ||
| } | ||
|
|
||
| // close waits out an in-flight registration (so Shutdown deregisters it) and | ||
| // refuses any later one. | ||
| func (g *gate) close() { | ||
| g.mu.Lock() | ||
| g.shut = true | ||
| g.mu.Unlock() | ||
| } | ||
|
|
||
| func newServer(name, version, socket string, client osquery.ExtensionManager, timeout time.Duration, plugins ...osquery.OsqueryPlugin) (*osquery.ExtensionManagerServer, *gate, error) { | ||
| g := &gate{ExtensionManager: client} | ||
| server, err := osquery.NewExtensionManagerServer( | ||
| name, | ||
| socket, | ||
| osquery.WithClient(g), | ||
| osquery.ServerTimeout(timeout), | ||
| osquery.ExtensionVersion(version), | ||
| ) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| server.RegisterPlugin(plugins...) | ||
| return server, g, nil | ||
| } | ||
|
|
||
| // run registers the extension and serves until ctx is cancelled, osquery asks | ||
| // it to shut down, or maxPingFailures consecutive pings fail. | ||
| func run(ctx context.Context, server *osquery.ExtensionManagerServer, client *gate, interval time.Duration) error { | ||
| // Always deregister on the way out so osqueryd never has to reap a stale | ||
| // registration itself. | ||
| defer func() { | ||
| client.close() | ||
| if err := server.Shutdown(context.Background()); err != nil { | ||
| log.Println(err) | ||
| } | ||
| }() | ||
| ctx, cancel := context.WithCancel(ctx) | ||
| defer cancel() // runs before Shutdown: stops registration retries | ||
|
|
||
| served := make(chan error, 1) | ||
| go func() { served <- start(ctx, server, interval) }() | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| ticker := time.NewTicker(interval) | ||
| defer ticker.Stop() | ||
| failures := 0 | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return nil | ||
| case err := <-served: | ||
| return err | ||
| case <-ticker.C: | ||
| status, err := client.Ping() | ||
| if err == nil && status.Code != 0 { | ||
| err = fmt.Errorf("status %d: %s", status.Code, status.Message) | ||
| } | ||
| if err == nil { | ||
| failures = 0 | ||
| continue | ||
| } | ||
| failures++ | ||
| if failures >= maxPingFailures { | ||
| return fmt.Errorf("osquery unreachable, %d pings failed: %w", failures, err) | ||
| } | ||
| log.Printf("ping %d/%d failed: %v", failures, maxPingFailures, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // start registers and serves. While osqueryd still holds a stale registration | ||
| // with our name it refuses us as a duplicate; its extension watcher normally | ||
| // reaps the stale entry within a couple of --extensions_interval periods, so | ||
| // retry in-process with backoff instead of exiting into a respawn loop. | ||
| func start(ctx context.Context, server *osquery.ExtensionManagerServer, backoff time.Duration) error { | ||
| for { | ||
| err := server.Start() | ||
| if err == nil || !strings.Contains(err.Error(), "Duplicate extension registered") { | ||
| return err | ||
| } | ||
| log.Printf("%v; retrying in %s", err, backoff) | ||
| select { | ||
| case <-ctx.Done(): | ||
| return nil | ||
| case <-time.After(backoff): | ||
| } | ||
| backoff = min(2*backoff, maxRetryBackoff) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.