Skip to content

Commit 01de174

Browse files
committed
Fixed an extremely rare race-condition during compile (#2512)
1 parent 5edfa98 commit 01de174

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

Diff for: arduino/builder/internal/compilation/database.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"os"
22+
"sync"
2223

2324
"github.com/arduino/arduino-cli/executils"
2425
"github.com/arduino/arduino-cli/i18n"
@@ -29,8 +30,9 @@ var tr = i18n.Tr
2930

3031
// Database keeps track of all the compile commands run by the builder
3132
type Database struct {
32-
Contents []Command
33-
File *paths.Path
33+
lock sync.Mutex
34+
contents []Command
35+
file *paths.Path
3436
}
3537

3638
// Command keeps track of a single run of a compile command
@@ -44,8 +46,8 @@ type Command struct {
4446
// NewDatabase creates an empty CompilationDatabase
4547
func NewDatabase(filename *paths.Path) *Database {
4648
return &Database{
47-
File: filename,
48-
Contents: []Command{},
49+
file: filename,
50+
contents: []Command{},
4951
}
5052
}
5153

@@ -56,16 +58,18 @@ func LoadDatabase(file *paths.Path) (*Database, error) {
5658
return nil, err
5759
}
5860
res := NewDatabase(file)
59-
return res, json.Unmarshal(f, &res.Contents)
61+
return res, json.Unmarshal(f, &res.contents)
6062
}
6163

6264
// SaveToFile save the CompilationDatabase to file as a clangd-compatible compile_commands.json,
6365
// see https://clang.llvm.org/docs/JSONCompilationDatabase.html
6466
func (db *Database) SaveToFile() {
65-
if jsonContents, err := json.MarshalIndent(db.Contents, "", " "); err != nil {
67+
db.lock.Lock()
68+
defer db.lock.Unlock()
69+
if jsonContents, err := json.MarshalIndent(db.contents, "", " "); err != nil {
6670
fmt.Println(tr("Error serializing compilation database: %s", err))
6771
return
68-
} else if err := db.File.WriteFile(jsonContents); err != nil {
72+
} else if err := db.file.WriteFile(jsonContents); err != nil {
6973
fmt.Println(tr("Error writing compilation database: %s", err))
7074
}
7175
}
@@ -89,5 +93,7 @@ func (db *Database) Add(target *paths.Path, command *executils.Process) {
8993
File: target.String(),
9094
}
9195

92-
db.Contents = append(db.Contents, entry)
96+
db.lock.Lock()
97+
db.contents = append(db.contents, entry)
98+
db.lock.Unlock()
9399
}

Diff for: arduino/builder/internal/compilation/database_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func TestCompilationDatabase(t *testing.T) {
3737
db2, err := LoadDatabase(tmpfile)
3838
require.NoError(t, err)
3939
require.Equal(t, db, db2)
40-
require.Len(t, db2.Contents, 1)
41-
require.Equal(t, db2.Contents[0].File, "test")
42-
require.Equal(t, db2.Contents[0].Command, "")
43-
require.Equal(t, db2.Contents[0].Arguments, []string{"gcc", "arg1", "arg2"})
40+
require.Len(t, db2.contents, 1)
41+
require.Equal(t, db2.contents[0].File, "test")
42+
require.Equal(t, db2.contents[0].Command, "")
43+
require.Equal(t, db2.contents[0].Arguments, []string{"gcc", "arg1", "arg2"})
4444
cwd, err := paths.Getwd()
4545
require.NoError(t, err)
46-
require.Equal(t, db2.Contents[0].Directory, cwd.String())
46+
require.Equal(t, db2.contents[0].Directory, cwd.String())
4747
}

0 commit comments

Comments
 (0)