@@ -308,20 +308,33 @@ func (n *Netns) setupSlirp4netns(nsPath string) error {
308308 return nil
309309}
310310
311+ var (
312+ // ErrEmptyPIDFile is returned when the PID file is empty.
313+ ErrEmptyPIDFile = errors .New ("PID file is empty" )
314+ )
315+
311316func (n * Netns ) cleanupRootlessNetns () error {
312317 pidFile := n .getPath (rootlessNetNsConnPidFile )
318+
313319 pid , err := readPidFile (pidFile )
314- // do not hard error if the file dos not exists , cleanup should be idempotent
320+ // do not hard error if the file does not exist , cleanup should be idempotent
315321 if errors .Is (err , fs .ErrNotExist ) {
316322 logrus .Debugf ("Rootless netns conn pid file does not exists %s" , pidFile )
317323 return nil
318324 }
319- if err == nil {
320- // kill the slirp/pasta process so we do not leak it
321- err = unix .Kill (pid , unix .SIGTERM )
322- if err == unix .ESRCH {
323- err = nil
324- }
325+ // if the pid file is empty, pasta failed to start so there is nothing to clean up
326+ if errors .Is (err , ErrEmptyPIDFile ) {
327+ logrus .Debugf ("Rootless netns conn pid file is empty, nothing to clean up" )
328+ return nil
329+ }
330+ if err != nil {
331+ logrus .Warnf ("Rootless netns conn pid file is invalid: %v" , err )
332+ return nil
333+ }
334+ // kill the slirp/pasta process so we do not leak it
335+ err = unix .Kill (pid , unix .SIGTERM )
336+ if err == unix .ESRCH {
337+ err = nil
325338 }
326339 return err
327340}
@@ -652,7 +665,11 @@ func readPidFile(path string) (int, error) {
652665 if err != nil {
653666 return 0 , err
654667 }
655- return strconv .Atoi (strings .TrimSpace (string (b )))
668+ s := strings .TrimSpace (string (b ))
669+ if s == "" {
670+ return 0 , ErrEmptyPIDFile
671+ }
672+ return strconv .Atoi (s )
656673}
657674
658675func (n * Netns ) serializeInfo () error {
0 commit comments