-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sockerpair: avoid double close(), set FD_CLOEXEC
We were calling the close() syscall multiple time with the same fd number, leading to random issues like closing containerd stream port. In newLaunchedPlugin() we have: sockets, _ := net.NewSocketPair() defer sockets.Close() conn, _ := sockets.LocalConn() peerFile := sockets.PeerFile() defer func() { peerFile.Close() if retErr != nil { conn.Close() } }() cmd.Start() so we were doing: close(local) (in LocalConn()) cmd.Start() close(peer) (peerFile.Close()) close(local) (sockets.Close()) close(peer) (sockets.Close()) If the NRI plugin that we launch with cmd.Start() is not cached or the system is a bit busy, cmd.Start() gives a large enough window for another goroutine to open a file that will get the same fd number as local was, thus being closed by accident. Fix the situation by storing os.Files instead of ints, so that closing multiple times just returns an error (that we ignore). Also set FD_CLOEXEC on the sockets so we don't leak them. Fixes 1da2cdf Signed-off-by: Etienne Champetier <[email protected]>
- Loading branch information
Showing
3 changed files
with
102 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package net | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
// SocketPair contains the os.Files of a connected pair of sockets. | ||
type SocketPair struct { | ||
local, peer *os.File | ||
} | ||
|
||
// LocalFile returns the socketpair fd for local usage as an *os.File. | ||
func (sp SocketPair) LocalFile() *os.File { | ||
return sp.local | ||
} | ||
|
||
// PeerFile returns the socketpair fd for peer usage as an *os.File. | ||
func (sp SocketPair) PeerFile() *os.File { | ||
return sp.peer | ||
} | ||
|
||
// LocalConn returns a net.Conn for the local end of the socketpair. | ||
func (sp SocketPair) LocalConn() (net.Conn, error) { | ||
file := sp.LocalFile() | ||
defer file.Close() | ||
conn, err := net.FileConn(file) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create net.Conn for %s: %w", file.Name(), err) | ||
} | ||
return conn, nil | ||
} | ||
|
||
// PeerConn returns a net.Conn for the peer end of the socketpair. | ||
func (sp SocketPair) PeerConn() (net.Conn, error) { | ||
file := sp.PeerFile() | ||
defer file.Close() | ||
conn, err := net.FileConn(file) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create net.Conn for %s: %w", file.Name(), err) | ||
} | ||
return conn, nil | ||
} | ||
|
||
// Close closes both ends of the socketpair. | ||
func (sp SocketPair) Close() { | ||
sp.LocalClose() | ||
sp.PeerClose() | ||
} | ||
|
||
// LocalClose closes the local end of the socketpair. | ||
func (sp SocketPair) LocalClose() { | ||
sp.local.Close() | ||
} | ||
|
||
// PeerClose closes the peer end of the socketpair. | ||
func (sp SocketPair) PeerClose() { | ||
sp.peer.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters