fix: stop endpoint handles missing/dead containers gracefully

This commit is contained in:
Zac Gaetano 2026-05-22 11:32:44 -04:00
parent 6510871448
commit 994fd799d0

View file

@ -450,7 +450,13 @@ router.post('/:id/stop', async (req, res, next) => {
const recorder = recorderResult.rows[0];
if (!recorder.container_id) {
return res.status(400).json({ error: 'No container running' });
// No container tracked — reset stuck status gracefully.
const result = await pool.query(
`UPDATE recorders SET container_id = NULL, status = 'stopped', updated_at = NOW()
WHERE id = $1 RETURNING *`,
[id]
);
return res.json(result.rows[0]);
}
const { remote: isRemote, apiUrl: targetNodeApiUrl } = await resolveNodeTarget(recorder.node_id);
@ -469,23 +475,27 @@ router.post('/:id/stop', async (req, res, next) => {
`/containers/${recorder.container_id}/stop`
);
if (stopRes.status !== 204 && stopRes.status !== 304) {
// 204 = stopped, 304 = already stopped, 404 = container gone — all acceptable.
if (stopRes.status !== 204 && stopRes.status !== 304 && stopRes.status !== 404) {
return res.status(500).json({
error: 'Failed to stop container',
details: stopRes.data,
});
}
const removeRes = await dockerApi(
'DELETE',
`/containers/${recorder.container_id}`
);
// Only attempt remove if the container existed (not 404).
if (stopRes.status !== 404) {
const removeRes = await dockerApi(
'DELETE',
`/containers/${recorder.container_id}`
);
if (removeRes.status !== 204 && removeRes.status !== 404) {
return res.status(500).json({
error: 'Failed to remove container',
details: removeRes.data,
});
if (removeRes.status !== 204 && removeRes.status !== 404) {
return res.status(500).json({
error: 'Failed to remove container',
details: removeRes.data,
});
}
}
}