@@ -8,20 +8,16 @@ import (
88 "errors"
99 "fmt"
1010 "io"
11- "math"
1211 "os"
1312 "os/exec"
1413 "path/filepath"
15- "slices"
1614 "sort"
1715 "strconv"
1816 "strings"
1917 "sync"
20- "syscall"
2118 "time"
2219
2320 "github.com/sirupsen/logrus"
24- "k8s.io/apimachinery/pkg/util/wait"
2521
2622 "github.com/k0sproject/k0s/internal/pkg/dir"
2723 "github.com/k0sproject/k0s/internal/pkg/log"
@@ -241,94 +237,7 @@ func (s *Supervisor) maybeCleanupPIDFile() error {
241237 return fmt .Errorf ("failed to parse PID file %s: %w" , s .PidFile , err )
242238 }
243239
244- ph , err := openPID (p )
245- if err != nil {
246- if errors .Is (err , syscall .ESRCH ) {
247- return nil // no such process, nothing to cleanup
248- }
249- return fmt .Errorf ("cannot interact with PID %d from PID file %s: %w" , p , s .PidFile , err )
250- }
251- defer ph .Close ()
252-
253- if managed , err := s .isK0sManaged (ph ); err != nil {
254- if errors .Is (err , os .ErrProcessDone ) {
255- return nil
256- }
257- return err
258- } else if ! managed {
259- return nil
260- }
261-
262- if err := s .terminateAndWait (ph ); err != nil {
263- return fmt .Errorf ("while waiting for termination of PID %d from PID file %s: %w" , p , s .PidFile , err )
264- }
265-
266- return nil
267- }
268-
269- // Tries to gracefully terminate a process and waits for it to exit. If the
270- // process is still running after several attempts, it returns an error instead
271- // of forcefully killing the process.
272- func (s * Supervisor ) terminateAndWait (ph procHandle ) error {
273- if err := ph .requestGracefulTermination (); err != nil {
274- if errors .Is (err , os .ErrProcessDone ) {
275- return nil
276- }
277- return fmt .Errorf ("failed to request graceful termination: %w" , err )
278- }
279-
280- errTimeout := errors .New ("process did not terminate in time" )
281- ctx , cancel := context .WithTimeoutCause (context .TODO (), s .TimeoutStop , errTimeout )
282- defer cancel ()
283- return s .awaitTermination (ctx , ph )
284- }
285-
286- // Checks if the process handle refers to a k0s-managed process. A process is
287- // considered k0s-managed if:
288- // - The executable path matches.
289- // - The process environment contains `_K0S_MANAGED=yes`.
290- func (s * Supervisor ) isK0sManaged (ph procHandle ) (bool , error ) {
291- if cmd , err := ph .cmdline (); err != nil {
292- // Only error out if the error doesn't indicate that getting the command
293- // line is unsupported. In that case, ignore the error and proceed to
294- // the environment check.
295- if ! errors .Is (err , errors .ErrUnsupported ) {
296- return false , err
297- }
298- } else if len (cmd ) > 0 && cmd [0 ] != s .BinPath {
299- return false , nil
300- }
301-
302- if env , err := ph .environ (); err != nil {
303- return false , err
304- } else if ! slices .Contains (env , k0sManaged ) {
305- return false , nil
306- }
307-
308- return true , nil
309- }
310-
311- func (s * Supervisor ) awaitTermination (ctx context.Context , ph procHandle ) error {
312- s .log .Debug ("Polling for process termination" )
313- backoff := wait.Backoff {
314- Duration : 25 * time .Millisecond ,
315- Cap : 3 * time .Second ,
316- Steps : math .MaxInt32 ,
317- Factor : 1.5 ,
318- Jitter : 0.1 ,
319- }
320-
321- if err := wait .ExponentialBackoffWithContext (ctx , backoff , func (context.Context ) (bool , error ) {
322- return ph .hasTerminated ()
323- }); err != nil {
324- if err == ctx .Err () { //nolint:errorlint // the equal check is intended
325- return context .Cause (ctx )
326- }
327-
328- return err
329- }
330-
331- return nil
240+ return s .cleanupPID (p )
332241}
333242
334243// Prepare the env for exec:
0 commit comments