File tree Expand file tree Collapse file tree 6 files changed +48
-19
lines changed
jsonutils/adapters/stdlib/json Expand file tree Collapse file tree 6 files changed +48
-19
lines changed Original file line number Diff line number Diff line change @@ -102,10 +102,6 @@ jobs:
102
102
103
103
steps :
104
104
- uses : actions/checkout@v5
105
- # - name: Ensure cache is available
106
- # run: |
107
- # mkdir -p /home/runner/go/pkg/mod
108
- # mkdir -p /home/runner/.cache/go.build
109
105
- uses : actions/setup-go@v6
110
106
with :
111
107
go-version : ' ${{ matrix.go_version }}'
@@ -119,6 +115,7 @@ jobs:
119
115
# *.coverage.* pattern is automatically detected by codecov
120
116
COVER_PROFILE : ' all_modules.coverage.${{ matrix.os }}.${{ matrix.go_version }}.out'
121
117
run : |
118
+ # when go1.25 becomes the oldstable, we may replace this bash with "go work test"
122
119
declare -a ALL_MODULES
123
120
BASH_MAJOR=$(echo $BASH_VERSION|cut -d'.' -f1)
124
121
if [[ "${BASH_MAJOR}" -ge 4 ]] ; then
Original file line number Diff line number Diff line change
1
+ fix data race https://github.com/go-openapi/swag/actions/runs/17989156861/job/51174860188
Original file line number Diff line number Diff line change @@ -18,9 +18,10 @@ echo "Tagging all modules in repo ${root##*/}..."
18
18
19
19
while read module_location ; do
20
20
relative_location=${module_location# " $root " / }
21
+ relative_location=${relative_location# " $root " }
21
22
module_dir=${relative_location% " /go.mod" }
22
23
base_tag=" ${module_dir# " ./" } "
23
- if [[ " ${base_tag} " == " . " ]] ; then
24
+ if [[ " ${base_tag} " == " " ]] ; then
24
25
module_tag=" ${tag} " # e.g. "v0.24.0"
25
26
else
26
27
module_tag=" ${base_tag} /${tag} " # e.g. "mangling/v0.24.0"
Original file line number Diff line number Diff line change @@ -143,23 +143,18 @@ func newLexer(data []byte) *jlexer {
143
143
// current: undefToken,
144
144
next : undefToken ,
145
145
}
146
- if data != nil {
147
- l .buf = & bytesReader {
148
- buf : data ,
149
- }
150
- l .dec = stdjson .NewDecoder (l .buf ) // unfortunately, cannot pool this
146
+ l .buf = & bytesReader {
147
+ buf : data ,
151
148
}
149
+ l .dec = stdjson .NewDecoder (l .buf ) // unfortunately, cannot pool this
152
150
153
151
return l
154
152
}
155
153
156
154
func (l * jlexer ) Reset () {
157
- l .dec = nil
158
155
l .err = nil
159
156
l .next = undefToken
160
- if l .buf != nil {
161
- l .buf .Reset ()
162
- }
157
+ // leave l.dec and l.buf alone, since they are replaced at every Borrow
163
158
}
164
159
165
160
func (l * jlexer ) Error () error {
Original file line number Diff line number Diff line change @@ -16,8 +16,12 @@ package json
16
16
17
17
import (
18
18
stdjson "encoding/json"
19
+ "fmt"
20
+ "io"
21
+ "sync"
19
22
"testing"
20
23
24
+ "github.com/stretchr/testify/assert"
21
25
"github.com/stretchr/testify/require"
22
26
23
27
fixtures "github.com/go-openapi/swag/jsonutils/fixtures_test"
@@ -127,3 +131,35 @@ func TestLexerErrors(t *testing.T) {
127
131
})
128
132
}
129
133
}
134
+
135
+ func TestReproDataRace (t * testing.T ) {
136
+ t .Parallel ()
137
+ const parallelRoutines = 1000
138
+
139
+ // NOTE: with go1.25, use synctest.Test
140
+ var wg sync.WaitGroup
141
+
142
+ for range parallelRoutines {
143
+ wg .Add (1 )
144
+ go func () {
145
+ defer func () {
146
+ wg .Done ()
147
+ }()
148
+
149
+ toks := make ([]token , 0 , 4 )
150
+ buf := []byte (`{"test":"data"}` )
151
+ l := poolOfLexers .Borrow (buf )
152
+
153
+ for tok := l .NextToken (); tok != eofToken ; tok = l .NextToken () {
154
+ toks = append (toks , tok )
155
+ }
156
+ assert .Len (t , toks , 4 )
157
+ fmt .Fprintf (io .Discard , "%d" , len (toks ))
158
+ defer func () {
159
+ poolOfLexers .Redeem (l )
160
+ }()
161
+ }()
162
+ }
163
+
164
+ wg .Wait ()
165
+ }
Original file line number Diff line number Diff line change @@ -62,19 +62,18 @@ func (p *lexersPool) Borrow(data []byte) *jlexer {
62
62
ptr := p .Get ()
63
63
64
64
l := ptr .(* jlexer )
65
- l .Reset ()
66
-
67
65
l .buf = poolOfReaders .Borrow (data )
68
66
l .dec = json .NewDecoder (l .buf ) // cannot pool, not exposed by the encoding/json API
67
+ l .Reset ()
69
68
70
69
return l
71
70
}
72
71
73
72
func (p * lexersPool ) Redeem (l * jlexer ) {
74
73
l .dec = nil
75
- if l .buf != nil {
76
- poolOfReaders . Redeem ( l .buf )
77
- }
74
+ discard := l .buf
75
+ l .buf = nil
76
+ poolOfReaders . Redeem ( discard )
78
77
p .Put (l )
79
78
}
80
79
You can’t perform that action at this time.
0 commit comments