Skip to content

Commit fddacbb

Browse files
BBQBBQ
authored andcommitted
fix(containerd): keep task running when network setup fails
In containerd-in-docker mode, SetupNetwork fails because netns is unavailable. Previously this killed the task, making stdio MCP tools unusable. Now the task continues running with a warning log, since stdio MCP communication does not require networking.
1 parent c7d66f8 commit fddacbb

1 file changed

Lines changed: 24 additions & 34 deletions

File tree

internal/handlers/containerd.go

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -244,25 +244,21 @@ func (h *ContainerdHandler) CreateContainer(c echo.Context) error {
244244
if task, err := h.service.StartTask(ctx, containerID, &ctr.StartTaskOptions{
245245
UseStdio: false,
246246
}); err == nil {
247-
if netErr := ctr.SetupNetwork(ctx, task, containerID); netErr == nil {
248-
started = true
249-
if h.queries != nil {
250-
if pgBotID, parseErr := db.ParseUUID(botID); parseErr == nil {
251-
if dbErr := h.queries.UpdateContainerStarted(c.Request().Context(), pgBotID); dbErr != nil {
252-
h.logger.Error("failed to update container started status",
253-
slog.String("bot_id", botID), slog.Any("error", dbErr))
254-
}
255-
}
256-
}
257-
} else {
258-
if err := h.service.StopTask(ctx, containerID, &ctr.StopTaskOptions{Force: true}); err != nil {
259-
h.logger.Warn("cleanup: stop task failed", slog.String("container_id", containerID), slog.Any("error", err))
260-
}
261-
h.logger.Error("mcp container network setup failed",
247+
started = true
248+
if netErr := ctr.SetupNetwork(ctx, task, containerID); netErr != nil {
249+
h.logger.Warn("mcp container network setup failed, task kept running",
262250
slog.String("container_id", containerID),
263251
slog.Any("error", netErr),
264252
)
265253
}
254+
if h.queries != nil {
255+
if pgBotID, parseErr := db.ParseUUID(botID); parseErr == nil {
256+
if dbErr := h.queries.UpdateContainerStarted(c.Request().Context(), pgBotID); dbErr != nil {
257+
h.logger.Error("failed to update container started status",
258+
slog.String("bot_id", botID), slog.Any("error", dbErr))
259+
}
260+
}
261+
}
266262
} else {
267263
h.logger.Error("mcp container start failed",
268264
slog.String("container_id", containerID),
@@ -318,11 +314,9 @@ func (h *ContainerdHandler) ensureContainerAndTask(ctx context.Context, containe
318314
if err != nil {
319315
return err
320316
}
321-
if err := ctr.SetupNetwork(ctx, task, containerID); err != nil {
322-
if err := h.service.StopTask(ctx, containerID, &ctr.StopTaskOptions{Force: true}); err != nil {
323-
h.logger.Warn("cleanup: stop task failed", slog.String("container_id", containerID), slog.Any("error", err))
324-
}
325-
return err
317+
if netErr := ctr.SetupNetwork(ctx, task, containerID); netErr != nil {
318+
h.logger.Warn("network setup failed, task kept running",
319+
slog.String("container_id", containerID), slog.Any("error", netErr))
326320
}
327321
return nil
328322
}
@@ -761,25 +755,21 @@ func (h *ContainerdHandler) SetupBotContainer(ctx context.Context, botID string)
761755
if task, err := h.service.StartTask(ctx, containerID, &ctr.StartTaskOptions{
762756
UseStdio: false,
763757
}); err == nil {
764-
if netErr := ctr.SetupNetwork(ctx, task, containerID); netErr == nil {
765-
if h.queries != nil {
766-
if pgBotID, parseErr := db.ParseUUID(botID); parseErr == nil {
767-
if dbErr := h.queries.UpdateContainerStarted(ctx, pgBotID); dbErr != nil {
768-
h.logger.Error("setup bot container: failed to update container started status",
769-
slog.String("bot_id", botID), slog.Any("error", dbErr))
770-
}
771-
}
772-
}
773-
} else {
774-
if err := h.service.StopTask(ctx, containerID, &ctr.StopTaskOptions{Force: true}); err != nil {
775-
h.logger.Warn("cleanup: stop task failed", slog.String("container_id", containerID), slog.Any("error", err))
776-
}
777-
h.logger.Error("setup bot container: network setup failed",
758+
if netErr := ctr.SetupNetwork(ctx, task, containerID); netErr != nil {
759+
h.logger.Warn("setup bot container: network setup failed, task kept running",
778760
slog.String("bot_id", botID),
779761
slog.String("container_id", containerID),
780762
slog.Any("error", netErr),
781763
)
782764
}
765+
if h.queries != nil {
766+
if pgBotID, parseErr := db.ParseUUID(botID); parseErr == nil {
767+
if dbErr := h.queries.UpdateContainerStarted(ctx, pgBotID); dbErr != nil {
768+
h.logger.Error("setup bot container: failed to update container started status",
769+
slog.String("bot_id", botID), slog.Any("error", dbErr))
770+
}
771+
}
772+
}
783773
} else {
784774
h.logger.Error("setup bot container: task start failed",
785775
slog.String("bot_id", botID),

0 commit comments

Comments
 (0)