Skip to content

Commit 925bc6f

Browse files
authored
Merge pull request docker#10393 from milas/fix-watch-segfault
watch: data race / segfault fixes
2 parents 6bedc19 + 16d5354 commit 925bc6f

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

pkg/compose/convergence.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ func (s *composeService) recreateContainer(ctx context.Context, project *types.P
455455

456456
// setDependentLifecycle define the Lifecycle strategy for all services to depend on specified service
457457
func setDependentLifecycle(project *types.Project, service string, strategy string) {
458+
mu.Lock()
459+
defer mu.Unlock()
460+
458461
for i, s := range project.Services {
459462
if utils.StringContains(s.GetDependencies(), service) {
460463
if s.Extensions == nil {

pkg/compose/watch.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
133133
}
134134
ignore := watch.NewCompositeMatcher(
135135
dockerIgnores,
136-
watch.EphemeralPathMatcher,
136+
watch.EphemeralPathMatcher(),
137137
dotGitIgnore,
138138
)
139139

@@ -336,17 +336,17 @@ type rebuildServices map[string]utils.Set[string]
336336

337337
func debounce(ctx context.Context, clock clockwork.Clock, delay time.Duration, input <-chan fileMapping, fn func(services rebuildServices)) {
338338
services := make(rebuildServices)
339-
t := clock.AfterFunc(delay, func() {
340-
if len(services) > 0 {
341-
fn(services)
342-
// TODO(milas): this is a data race!
343-
services = make(rebuildServices)
344-
}
345-
})
339+
t := clock.NewTimer(delay)
340+
defer t.Stop()
346341
for {
347342
select {
348343
case <-ctx.Done():
349344
return
345+
case <-t.Chan():
346+
if len(services) > 0 {
347+
go fn(services)
348+
services = make(rebuildServices)
349+
}
350350
case e := <-input:
351351
t.Reset(delay)
352352
svc, ok := services[e.Service]

pkg/watch/ephemeral.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ package watch
2424
// stop-gap so they don't have a terrible experience if those files aren't
2525
// there or aren't in the right places.
2626
//
27-
// https://app.clubhouse.io/windmill/story/691/filter-out-ephemeral-file-changes
28-
var EphemeralPathMatcher = initEphemeralPathMatcher()
29-
30-
func initEphemeralPathMatcher() PathMatcher {
27+
// NOTE: The underlying `patternmatcher` is NOT always Goroutine-safe, so
28+
// this is not a singleton; we create an instance for each watcher currently.
29+
func EphemeralPathMatcher() PathMatcher {
3130
golandPatterns := []string{"**/*___jb_old___", "**/*___jb_tmp___", "**/.idea/**"}
3231
emacsPatterns := []string{"**/.#*", "**/#*#"}
3332
// if .swp is taken (presumably because multiple vims are running in that dir),

pkg/watch/ephemeral_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2023 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package watch_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
23+
"github.com/docker/compose/v2/pkg/watch"
24+
)
25+
26+
func TestEphemeralPathMatcher(t *testing.T) {
27+
ignored := []string{
28+
".file.txt.swp",
29+
"/path/file.txt~",
30+
"/home/moby/proj/.idea/modules.xml",
31+
".#file.txt",
32+
"#file.txt#",
33+
"/dir/.file.txt.kate-swp",
34+
"/go/app/1234-go-tmp-umask",
35+
}
36+
matcher := watch.EphemeralPathMatcher()
37+
for _, p := range ignored {
38+
ok, err := matcher.Matches(p)
39+
if assert.NoErrorf(t, err, "Matching %s", p) {
40+
assert.Truef(t, ok, "Path %s should have matched", p)
41+
}
42+
}
43+
44+
const includedPath = "normal.txt"
45+
ok, err := matcher.Matches(includedPath)
46+
if assert.NoErrorf(t, err, "Matching %s", includedPath) {
47+
assert.Falsef(t, ok, "Path %s should NOT have matched", includedPath)
48+
}
49+
}

0 commit comments

Comments
 (0)