Skip to content

fix(start): use image digest for rebuild detection, fix rand and ssh-agent data races, for #8145 - #8359

Merged
stasadev merged 2 commits into
ddev:mainfrom
stasadev:20260428_stasadev_rand_and_hash
Apr 29, 2026
Merged

stasadev merged 2 commits into
ddev:mainfrom
stasadev:20260428_stasadev_rand_and_hash

Conversation

@stasadev

@stasadev stasadev commented Apr 28, 2026 •

Copy link
Copy Markdown
Member

The Issue

  1. When a Docker image tag is moved to a different image (e.g. after a second push or a docker tag), ddev start doesn't rebuild the project images because the old fingerprint still matches the tag name.

  2. Race condition in https://github.com/ddev/ddev/actions/runs/25064604457/job/73428302255#step:13:563

    Two concurrent goroutines (PopulateGlobalCustomCommandFiles and StartDdevRouter → PushGlobalTraefikConfig → ListFilesInVolume) both call RandomString(), which uses a shared *rand.Rand instance (seededRand) without locking:

    WARNING: DATA RACE
    Read at 0x00c0002bf500 by goroutine 122:
      math/rand.(*rngSource).Uint64()
          /opt/hostedtoolcache/go/1.26.2/x64/src/math/rand/rng.go:239 +0x30
      ...
      github.com/ddev/ddev/pkg/nodeps.RandomString()
          /home/runner/work/ddev/ddev/pkg/nodeps/utils.go:64 +0x28a
      github.com/ddev/ddev/pkg/ddevapp.performTaskInContainer()
          /home/runner/work/ddev/ddev/pkg/ddevapp/commands.go:86 +0x2b0
      github.com/ddev/ddev/pkg/ddevapp.PopulateGlobalCustomCommandFiles()
          /home/runner/work/ddev/ddev/pkg/ddevapp/commands.go:57 +0x46b
      github.com/ddev/ddev/pkg/ddevapp.(*DdevApp).Start.func8()
          /home/runner/work/ddev/ddev/pkg/ddevapp/ddevapp.go:2139 +0x29
    
    Previous write at 0x00c0002bf500 by goroutine 123:
      math/rand.(*rngSource).Uint64()
          /opt/hostedtoolcache/go/1.26.2/x64/src/math/rand/rng.go:239 +0x44
      ...
      github.com/ddev/ddev/pkg/nodeps.RandomString()
          /home/runner/work/ddev/ddev/pkg/nodeps/utils.go:64 +0x144
      github.com/ddev/ddev/pkg/dockerutil.ListFilesInVolume()
          /home/runner/work/ddev/ddev/pkg/dockerutil/volumes.go:266 +0x16e
      github.com/ddev/ddev/pkg/ddevapp.PushGlobalTraefikConfig()
          /home/runner/work/ddev/ddev/pkg/ddevapp/traefik.go:397 +0x4c0d
      github.com/ddev/ddev/pkg/ddevapp.StartDdevRouter()
          /home/runner/work/ddev/ddev/pkg/ddevapp/router.go:150 +0xc66
      github.com/ddev/ddev/pkg/ddevapp.(*DdevApp).Start.func9()
          /home/runner/work/ddev/ddev/pkg/ddevapp/ddevapp.go:2145 +0x66
    
  3. Race condition in https://github.com/ddev/ddev/actions/runs/25065414461/job/73431107278#step:13:16538

    The chown goroutine called EnsureSSHAgentContainer() after finishing chown, but EnsureSSHAgentContainer() reads app.ComposeYaml while the main goroutine was concurrently writing it inside WriteDockerComposeYAML():

    WARNING: DATA RACE
    Read at 0x00c0002e47b0 by goroutine 12619:
      github.com/ddev/ddev/pkg/ddevapp.(*DdevApp).GetWebEnvVar()
          /home/runner/work/ddev/ddev/pkg/ddevapp/ddevapp.go:734 +0xf5
      ...
      github.com/ddev/ddev/pkg/ddevapp.(*DdevApp).EnsureSSHAgentContainer()
          /home/runner/work/ddev/ddev/pkg/ddevapp/ssh_auth.go:55 +0x6c5
      github.com/ddev/ddev/pkg/ddevapp.(*DdevApp).Start.func4()
          /home/runner/work/ddev/ddev/pkg/ddevapp/ddevapp.go:1790 +0x6f8
    
    Previous write at 0x00c0002e47b0 by goroutine 12613:
      github.com/ddev/ddev/pkg/ddevapp.(*DdevApp).WriteDockerComposeYAML()
          /home/runner/work/ddev/ddev/pkg/ddevapp/compose_yaml.go:85 +0x94a
      github.com/ddev/ddev/pkg/ddevapp.(*DdevApp).Start()
          /home/runner/work/ddev/ddev/pkg/ddevapp/ddevapp.go:1814 +0x43ca
    

How This PR Solves The Issue

For 1: buildContextFingerprint() now calls dockerutil.ImageID() to resolve each tag to its sha256: content-addressable digest before hashing. If a tag is re-pointed to a different image, the digest changes and the build is triggered. pullWg.Wait() is placed before the fingerprint check so the digest reflects the freshly-pulled image rather than a stale local one.

For 2: Remove the shared *rand.Rand instance (seededRand) and replace all calls with the package-level rand.Intn(), which has been auto-seeded and internally locked since Go 1.20. This project requires Go 1.25, so it is guaranteed safe, see https://pkg.go.dev/math/rand#Seed

For 3: Move EnsureSSHAgentContainer() out of the chown goroutine and run it sequentially after chownWg.Wait(), which happens after WriteDockerComposeYAML() completes. This preserves the chown parallelism while eliminating the read/write race on app.ComposeYaml.

Manual Testing Instructions

Using this PR, the webserver image is rebuilt on tag change:

$ ddev poweroff

$ ddev version -j | jq -r .raw.web
ddev/ddev-webserver:20260427_stasadev_dockerfile

$ ddev start

$ docker pull ddev/ddev-webserver:v1.25.2

$ docker images ddev/ddev-webserver:v1.25.2 -q
fc4767e92965

$ docker images ddev/ddev-webserver:20260427_stasadev_dockerfile -q
d19724abd31d

$ docker tag ddev/ddev-webserver:v1.25.2 ddev/ddev-webserver:20260427_stasadev_dockerfile

# now 20260427_stasadev_dockerfile points to a different image
$ docker images ddev/ddev-webserver:20260427_stasadev_dockerfile -q
fc4767e92965

$ ddev start
...
Building project images............
...

$ ddev start
# no "Building project images..." — hash is unchanged

# move the tag back

$ docker pull ddev/ddev-webserver:20260427_stasadev_dockerfile

$ docker images ddev/ddev-webserver:20260427_stasadev_dockerfile -q
d19724abd31d

$ ddev start
...
Building project images.....
...

@stasadev

This comment was marked as resolved.

@github-actions

github-actions Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

@stasadev
stasadev force-pushed the 20260428_stasadev_rand_and_hash branch from 941cc5a to 67ce072 Compare April 29, 2026 08:36
@stasadev stasadev changed the title fix(start): calculate image hash and fix race in util.RandomString, for #8145 fix(start): use image digest for rebuild detection, fix rand and ssh-agent data races, for #8145 Apr 29, 2026
@stasadev
stasadev requested a review from Copilot April 29, 2026 08:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes ddev start correctness regressions introduced with the recent startup optimizations by making rebuild detection robust to retagging and eliminating two startup-time data races.

Changes:

  • Switch build-skip fingerprinting to hash base image IDs (via dockerutil.ImageID) instead of tag strings, and wait for background pulls before fingerprinting.
  • Remove shared *rand.Rand usage in nodeps.RandomString() to eliminate a race by relying on the package-level math/rand functions.
  • Reorder SSH-agent container setup to run after the chown goroutine completes to avoid a race on app.ComposeYaml.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/nodeps/utils.go Removes shared RNG state from RandomString() to eliminate concurrent access races.
pkg/dockerutil/images.go Adds ImageID() helper to resolve a local image’s content-addressable ID for fingerprinting.
pkg/ddevapp/ddevapp.go Uses image IDs for build-context fingerprinting; adjusts startup ordering to avoid SSH-agent/ComposeYaml race.

Comment thread pkg/dockerutil/images.go Resolved
Comment thread pkg/ddevapp/ddevapp.go
Comment on lines +1489 to 1500
// buildContextFingerprint returns an SHA-256 hash of the build context directories
// (.webimageBuild, .dbimageBuild) and base image IDs. This is used to detect
// when docker-compose build can be skipped because nothing has changed.
// Image IDs (sha256 digests) are used instead of tag names so that
// rebuilt images with the same tag are still detected as changed.
func (app *DdevApp) buildContextFingerprint() string {
dirs := []string{
app.GetConfigPath(".webimageBuild"),
app.GetConfigPath(".dbimageBuild"),
}
hash, err := fileutil.HashDirs(dirs, app.WebImage, app.GetDBImage())
hash, err := fileutil.HashDirs(dirs, dockerutil.ImageID(app.WebImage), dockerutil.ImageID(app.GetDBImage()))
if err != nil {

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the build-skip fingerprinting behavior (using ImageID/tag digest resolution and waiting for pulls before hashing), but there’s no test exercising the regression case where a base image tag is moved to a different image. Please add/extend a start-related integration test to assert that changing the underlying image for app.WebImage/app.GetDBImage() triggers a rebuild, and that subsequent starts do not rebuild when the digest is unchanged.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would require a full integration test with a live Docker daemon, image tagging, and a ddev start cycle - closer to a manual scenario than a unit test. The manual testing steps in the PR description cover this case. Skipping for now as this is a focused bugfix PR.

@stasadev
stasadev marked this pull request as ready for review April 29, 2026 09:42
@stasadev
stasadev requested a review from a team as a code owner April 29, 2026 09:42
@rfay
rfay requested a review from jonesrussell April 29, 2026 14:19

@rfay rfay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Masterful work, and such a small PR for such important fixes. thank you!

Comment thread pkg/ddevapp/ddevapp.go
// (.webimageBuild, .dbimageBuild) and base image IDs. This is used to detect
// when docker-compose build can be skipped because nothing has changed.
// Image IDs (sha256 digests) are used instead of tag names so that
// rebuilt images with the same tag are still detected as changed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is an example of when this happens? (same tag, new SHA)?

@stasadev stasadev Apr 29, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happened in:

I pushed the image several times there and was confused why I didn't see the changes after ddev utility download-images && ddev restart.

We don't need to re-push the image to test this behavior - the OP already has a reproduction using docker tag, which is basically the same thing.

@stasadev
stasadev merged commit daa2cce into ddev:main Apr 29, 2026
42 of 43 checks passed
@stasadev
stasadev deleted the 20260428_stasadev_rand_and_hash branch April 29, 2026 17:31
@stasadev

Copy link
Copy Markdown
Member Author

I think we need more sophisticated rebuild detection.

For example, these Docker-related values can change:

  • version
  • API version
  • Buildx version
  • context
  • host

All of them should be part of the build hash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants