stacklok / stacklok/toolhive

Handle concurrent default-group creation during startup

Open
#6,359 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
2.2k
Forks
300
Avg merge
1d 15h
Merged PRs (30d)
184

Description

Summary

thv startup can fail when multiple CLI processes initialize the default group concurrently. This is reproducible in the core E2E shard, which runs Ginkgo with PROCS=4.

Reproduction

Start four processes concurrently against the same fresh state directory:

for i in 1 2 3 4; do
  ./bin/thv --help &
done
wait

With a fresh shared XDG state directory, one process creates the default group and another process exits with:

failed to ensure default group exists
group already exists: default

Root cause

cmd/thv/main.go runs migration.EnsureDefaultGroupExists() for --help because --help is not classified as informational. pkg/migration/migration.go performs an Exists check followed by Create. Concurrent processes can all observe that the group is absent; one wins creation and the others receive groups.ErrGroupAlreadyExists. That expected race is returned as a fatal startup error.

The E2E helper then reports this misleadingly as thv binary not available because it only preserves the command error and discards stderr (test/e2e/helpers.go:367-372).

Expected behavior

If another process creates the default group first, startup should treat groups.ErrGroupAlreadyExists as success and continue.

Suggested validation

Add a regression test that invokes concurrent default-group initialization against a shared local store and verifies all callers succeed. Also preserve stderr in CheckTHVBinaryAvailable so startup failures are diagnosable.

Related PR: #6350

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with cmd/thv/main.go and pkg/migration/migration.go to trace default-group initialization, then inspect test/e2e/helpers.go:367-372 for stderr handling. Add the suggested concurrent shared-store regression test, treat groups.ErrGroupAlreadyExists as success, and preserve stderr so CheckTHVBinaryAvailable reports startup failures. Validate with the core E2E shard or four concurrent ./bin/thv --help processes.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend, cli, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.