daemon: Daemon.handleContainerExit(): reduce ambiguity in error handling

This goroutine was added in c458bca6dc, and
looks for errors from the wait channel. If no error is returned, it attempts
to start the container, and *updates* the error if a failure happened while
doing so, so that the code below it can update the container's status, and
perform auto-remove (if set for the container).

However, due to the formatting of the code, it was easy to overlook that
the "err" variable was not local to the "if" statement.

This patch breaks up the if-statement in an attempt to make it clearer that
this is not a local "err" variable, and adds a code-comment explaining the
logic.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-08-14 13:04:15 +02:00
parent cb36f57299
commit f8363690ca
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -121,7 +121,11 @@ func (daemon *Daemon) handleContainerExit(c *container.Container, e *libcontaine
// So to avoid panic at startup process, here must wait util daemon restore done.
daemon.waitForStartupDone()
cfg := daemon.config() // Apply the most up-to-date daemon config to the restarted container.
if err = daemon.containerStart(context.Background(), cfg, c, "", "", false); err != nil {
// update the error if we fail to start the container, so that the cleanup code
// below can handle updating the container's status, and auto-remove (if set).
err = daemon.containerStart(context.Background(), cfg, c, "", "", false)
if err != nil {
log.G(ctx).Debugf("failed to restart container: %+v", err)
}
}