Skip to content

Commit

Permalink
Merge pull request containernetworking#455 from booxter/master
Browse files Browse the repository at this point in the history
Unlock OS thread after netns is restored
  • Loading branch information
mccv1r0 authored Mar 4, 2020
2 parents 32fc3ee + 112288e commit 47a9fd8
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pkg/ns/ns_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,16 @@ func (ns *netNS) Do(toRun func(NetNS) error) error {
if err = ns.Set(); err != nil {
return fmt.Errorf("error switching to ns %v: %v", ns.file.Name(), err)
}
defer threadNS.Set() // switch back
defer func() {
err := threadNS.Set() // switch back
if err == nil {
// Unlock the current thread only when we successfully switched back
// to the original namespace; otherwise leave the thread locked which
// will force the runtime to scrap the current thread, that is maybe
// not as optimal but at least always safe to do.
runtime.UnlockOSThread()
}
}()

return toRun(hostNS)
}
Expand All @@ -193,6 +202,10 @@ func (ns *netNS) Do(toRun func(NetNS) error) error {
var wg sync.WaitGroup
wg.Add(1)

// Start the callback in a new green thread so that if we later fail
// to switch the namespace back to the original one, we can safely
// leave the thread locked to die without a risk of the current thread
// left lingering with incorrect namespace.
var innerError error
go func() {
defer wg.Done()
Expand Down

0 comments on commit 47a9fd8

Please sign in to comment.