-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Ensure we can always terminate the parent process on error #4355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lifubang
wants to merge
1
commit into
opencontainers:main
Choose a base branch
from
lifubang:fix-no-terminate-onerror
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -201,20 +201,31 @@ func (c *Container) Set(config configs.Config) error { | |
func (c *Container) Start(process *Process) error { | ||
c.m.Lock() | ||
defer c.m.Unlock() | ||
return c.start(process) | ||
|
||
if err := c.start(process); err != nil { | ||
c.terminate(process) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Run immediately starts the process inside the container. Returns an error if | ||
// the process fails to start. It does not block waiting for the exec fifo | ||
// after start returns but opens the fifo after start returns. | ||
func (c *Container) Run(process *Process) error { | ||
func (c *Container) Run(process *Process) (retErr error) { | ||
c.m.Lock() | ||
defer c.m.Unlock() | ||
|
||
if err := c.start(process); err != nil { | ||
c.terminate(process) | ||
return err | ||
} | ||
if process.Init { | ||
return c.exec() | ||
if !process.Init { | ||
return nil | ||
} | ||
if err := c.exec(); err != nil { | ||
c.terminate(process) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
@@ -226,6 +237,34 @@ func (c *Container) Exec() error { | |
return c.exec() | ||
} | ||
|
||
// terminate is to kill the container's init/exec process when got failure. | ||
func (c *Container) terminate(process *Process) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it simple to add tests? Maybe for this function. We can fake the process interface and make it return an error, and test everything that needs to happen, indeed happens? This might not add a lot of value now, but it will if we refactor this code in the future. |
||
if process.ops == nil { | ||
return | ||
} | ||
if process.Init { | ||
if err := ignoreTerminateErrors(process.ops.terminate()); err != nil { | ||
logrus.WithError(err).Warn("unable to terminate initProcess") | ||
} | ||
// If we haven't saved container's state yet, we need to destroy the | ||
// cgroup & intelRdt manager manually. | ||
if _, err := os.Stat(filepath.Join(c.stateDir, stateFilename)); os.IsNotExist(err) { | ||
if err := c.cgroupManager.Destroy(); err != nil { | ||
logrus.WithError(err).Warn("unable to destroy cgroupManager") | ||
} | ||
if c.intelRdtManager != nil { | ||
if err := c.intelRdtManager.Destroy(); err != nil { | ||
logrus.WithError(err).Warn("unable to destroy intelRdtManager") | ||
} | ||
} | ||
} | ||
return | ||
} | ||
if err := ignoreTerminateErrors(process.ops.terminate()); err != nil { | ||
logrus.WithError(err).Warn("unable to terminate setnsProcess") | ||
} | ||
} | ||
|
||
func (c *Container) exec() error { | ||
path := filepath.Join(c.stateDir, execFifoFilename) | ||
pid := c.initProcess.pid() | ||
|
@@ -356,12 +395,7 @@ func (c *Container) start(process *Process) (retErr error) { | |
return err | ||
} | ||
|
||
if err := c.config.Hooks.Run(configs.Poststart, s); err != nil { | ||
if err := ignoreTerminateErrors(parent.terminate()); err != nil { | ||
logrus.Warn(fmt.Errorf("error running poststart hook: %w", err)) | ||
} | ||
return err | ||
} | ||
return c.config.Hooks.Run(configs.Poststart, s) | ||
} | ||
} | ||
return nil | ||
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.