Skip to content

Commit 2c8a563

Browse files
committed
fix: Protect from concurrent calls to all instances of idempotent Close()
1 parent 491533a commit 2c8a563

File tree

10 files changed

+63
-4
lines changed

10 files changed

+63
-4
lines changed

pkg/chunks/pusher.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ type Pusher struct {
2525

2626
workerWg sync.WaitGroup
2727
workerSem chan struct{}
28-
errs chan error
28+
29+
closeLock sync.Mutex
30+
31+
errs chan error
2932
}
3033

3134
func NewPusher(
@@ -166,6 +169,9 @@ func (p *Pusher) Wait() error {
166169
}
167170

168171
func (p *Pusher) Close() error {
172+
p.closeLock.Lock()
173+
defer p.closeLock.Unlock()
174+
169175
if _, err := p.Sync(); err != nil {
170176
return err
171177
}

pkg/migration/file_migrator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package migration
33
import (
44
"context"
55
"os"
6+
"sync"
67

78
"github.com/pojntfx/go-nbd/pkg/backend"
89
"github.com/pojntfx/go-nbd/pkg/client"
@@ -26,6 +27,8 @@ type FileMigrator struct {
2627
leecher *FileLeecher
2728
released bool
2829

30+
closeLock sync.Mutex
31+
2932
errs chan error
3033
}
3134

@@ -253,6 +256,9 @@ func (s *FileMigrator) Leech(
253256
}
254257

255258
func (s *FileMigrator) Close() error {
259+
s.closeLock.Lock()
260+
defer s.closeLock.Unlock()
261+
256262
if s.seeder != nil {
257263
_ = s.seeder.Close()
258264
}

pkg/migration/path_leecher.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ type PathLeecher struct {
8787

8888
pullerWg sync.WaitGroup
8989
devWg *sync.WaitGroup
90-
errs chan error
90+
91+
closeLock sync.Mutex
92+
93+
errs chan error
9194
}
9295

9396
func NewPathLeecher(
@@ -341,6 +344,9 @@ func (l *PathLeecher) Release() (
341344
}
342345

343346
func (l *PathLeecher) Close() error {
347+
l.closeLock.Lock()
348+
defer l.closeLock.Unlock()
349+
344350
if !l.released {
345351
if hook := l.hooks.OnBeforeClose; hook != nil {
346352
if err := hook(); err != nil {

pkg/migration/path_migrator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package migration
33
import (
44
"context"
55
"errors"
6+
"sync"
67

78
"github.com/pojntfx/go-nbd/pkg/backend"
89
"github.com/pojntfx/go-nbd/pkg/client"
@@ -49,6 +50,8 @@ type PathMigrator struct {
4950
leecher *PathLeecher
5051
released bool
5152

53+
closeLock sync.Mutex
54+
5255
errs chan error
5356
}
5457

@@ -277,6 +280,9 @@ func (s *PathMigrator) Leech(
277280
}
278281

279282
func (s *PathMigrator) Close() error {
283+
s.closeLock.Lock()
284+
defer s.closeLock.Unlock()
285+
280286
if s.seeder != nil {
281287
_ = s.seeder.Close()
282288
}

pkg/migration/path_seeder.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ type PathSeeder struct {
4141

4242
devicePath string
4343

44-
wg *sync.WaitGroup
44+
wg *sync.WaitGroup
45+
46+
closeLock sync.Mutex
47+
4548
errs chan error
4649
}
4750

@@ -219,6 +222,9 @@ func (s *PathSeeder) Open() (string, int64, *services.SeederService, error) {
219222
}
220223

221224
func (s *PathSeeder) Close() error {
225+
s.closeLock.Lock()
226+
defer s.closeLock.Unlock()
227+
222228
if hook := s.hooks.OnBeforeClose; hook != nil {
223229
if err := hook(); err != nil {
224230
return err

pkg/migration/slice_migrator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package migration
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/pojntfx/go-nbd/pkg/backend"
78
"github.com/pojntfx/go-nbd/pkg/client"
@@ -25,6 +26,8 @@ type SliceMigrator struct {
2526
leecher *SliceLeecher
2627
released bool
2728

29+
closeLock sync.Mutex
30+
2831
errs chan error
2932
}
3033

@@ -252,6 +255,9 @@ func (s *SliceMigrator) Leech(
252255
}
253256

254257
func (s *SliceMigrator) Close() error {
258+
s.closeLock.Lock()
259+
defer s.closeLock.Unlock()
260+
255261
if s.seeder != nil {
256262
_ = s.seeder.Close()
257263
}

pkg/mount/file_direct.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mount
22

33
import (
44
"os"
5+
"sync"
56

67
"github.com/pojntfx/go-nbd/pkg/backend"
78
"github.com/pojntfx/go-nbd/pkg/client"
@@ -13,6 +14,8 @@ type DirectFileMount struct {
1314

1415
devicePath string
1516
deviceFile *os.File
17+
18+
closeLock sync.Mutex
1619
}
1720

1821
func NewDirectFileMount(
@@ -54,6 +57,9 @@ func (d *DirectFileMount) Open() (*os.File, error) {
5457
}
5558

5659
func (d *DirectFileMount) Close() error {
60+
d.closeLock.Lock()
61+
defer d.closeLock.Unlock()
62+
5763
if d.deviceFile != nil {
5864
_ = d.deviceFile.Close()
5965

pkg/mount/path_direct.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mount
33
import (
44
"net"
55
"os"
6+
"sync"
67
"syscall"
78

89
"github.com/pojntfx/go-nbd/pkg/backend"
@@ -24,6 +25,8 @@ type DirectPathMount struct {
2425
cf *os.File
2526
cc *net.UnixConn
2627

28+
closeLock sync.Mutex
29+
2730
errs chan error
2831
}
2932

@@ -126,6 +129,9 @@ func (d *DirectPathMount) Open() error {
126129
}
127130

128131
func (d *DirectPathMount) Close() error {
132+
d.closeLock.Lock()
133+
defer d.closeLock.Unlock()
134+
129135
_ = client.Disconnect(d.f)
130136

131137
if d.cc != nil {

pkg/mount/path_managed.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ type ManagedPathMount struct {
5353
puller *chunks.Puller
5454
dev *DirectPathMount
5555

56-
wg sync.WaitGroup
56+
wg sync.WaitGroup
57+
58+
closeLock sync.Mutex
59+
5760
errs chan error
5861
}
5962

@@ -279,6 +282,9 @@ func (m *ManagedPathMount) Open() (string, int64, error) {
279282
}
280283

281284
func (m *ManagedPathMount) Close() error {
285+
m.closeLock.Lock()
286+
defer m.closeLock.Unlock()
287+
282288
if m.syncer != nil {
283289
_ = m.syncer.Sync()
284290
}

pkg/mount/slice_direct.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type DirectSliceMount struct {
2020
mmapMount sync.Mutex
2121

2222
b backend.Backend
23+
24+
closeLock sync.Mutex
2325
}
2426

2527
func NewDirectSliceMount(
@@ -79,6 +81,9 @@ func (d *DirectSliceMount) Open() ([]byte, error) {
7981
}
8082

8183
func (d *DirectSliceMount) Close() error {
84+
d.closeLock.Lock()
85+
defer d.closeLock.Unlock()
86+
8287
d.mmapMount.Lock()
8388
if d.slice != nil {
8489
_ = d.slice.Unlock()

0 commit comments

Comments
 (0)