Skip to content

Commit 66f47e8

Browse files
committed
netstack/fdbased: linearize fd swaps w/ mutex
1 parent 27890ab commit 66f47e8

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

intra/netstack/fdbased.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,16 @@ func (e *endpoint) Dispose() (err error) {
243243
log.W("ns: tun: Dispose: no fds")
244244
return nil
245245
}
246+
247+
e.Lock()
248+
defer e.Unlock()
249+
246250
prevfd := e.fds.Swap(invalidfd) // prevfd may be invalidfd
247251
if prevfd == invalidfd {
248252
log.W("ns: tun: Dispose: invalid prevfd")
249253
return nil
250254
}
251-
e.Lock()
252-
defer e.Unlock()
255+
253256
if e.inboundDispatcher == nil {
254257
log.W("ns: tun: Dispose: no inbound dispatcher")
255258
// nothing to do
@@ -267,12 +270,12 @@ func (e *endpoint) Swap(fd int) (err error) {
267270
return fmt.Errorf("ns: tun: set non blocking(%d) failed: %v", fd, err)
268271
}
269272

270-
prevfd := e.fds.Swap(fd) // commence WritePackets() on fd
273+
e.Lock()
274+
defer e.Unlock()
271275

276+
prevfd := e.fds.Swap(fd) // commence WritePackets() on fd
272277
log.D("ns: swap: tun fd %d => %d", prevfd, fd)
273278

274-
e.Lock()
275-
defer e.Unlock()
276279
if e.inboundDispatcher == nil { // prevfd must be 0 value if inbound is nil
277280
e.inboundDispatcher, err = createInboundDispatcher(e, fd)
278281
} else {

0 commit comments

Comments
 (0)