← All Exercises

Clean Up Resources on Signal

basicjavascript~20min

Background

SIGTERM handling that skips cleanup leaves stale lock files and half-written outputs. Cleanup must run in reverse acquisition order to avoid dangling dependencies.

Problem

Given acquired resources in order and a terminating signal, return the reverse-order cleanup list and the conventional exit code.

Input

JSON object: { acquired: string[], signal: "TERM" | "INT" }

Output

JSON object: { cleanup: string[], exitCode: number }

Sample tests

Sample 1

Input: {"acquired":["lock","tmpdir","socket"],"signal":"TERM"}
Expected: {"cleanup":["socket","tmpdir","lock"],"exitCode":143}

Sample 2

Input: {"acquired":["lock"],"signal":"INT"}
Expected: {"cleanup":["lock"],"exitCode":130}

Your solution

Learning objective

Write signal handlers that leave no partial state behind.