6 min read

Eye Training · Resources and Cleanup

Everything you open, take or start must be given back on every path out: success, early return, thrown error and shutdown.

Part 2 · Surface 24. Bug counts are hidden and some snippets are clean. See Start Here for how to use these drills.


J · The basics

Bug hunt
J · Sync button
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Find a user
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Find an order
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min

M · Three years in, these should jump out

Bug hunt
M · Image preview
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · PDF conversion
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Cache sweeper
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Thumbnails
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min

S · Where seniors stand out

Bug hunt
S · Transfer
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
S · Shutdown
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
S · Compress a log
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min

What your eye checks

Whenever code opens, takes or starts something, find where it's given back, then ask:

  1. Is it released on every path? Early returns and thrown errors skip the last line. Releases belong in finally.
  2. Does a flag or lock get reset on failure? A stuck busy is a resource leak too.
  3. Is a transaction rolled back? Releasing a connection isn't enough if the transaction is still open.
  4. Is there a limit? Threads, connections, child processes and open files need a pool or a cap.
  5. Does importing it start anything? Timers and sockets started at import time keep the process alive.
  6. Are names unique? Temp files and keys built from Date.now() collide.
  7. Does shutdown wait? Stop taking work, finish what's in flight, then close what it uses.
  8. Is there a helper that does the cleanup for you? pool.query, pipeline, fs.rm with force.

Tells

You seeAsk
pool.connect()Where's release, and is it in finally?
return between acquire and releaseReleased on this path?
busy = true … busy = falseWhat if it throws in between?
BEGIN without a ROLLBACKWhat does the next user of this connection get?
URL.createObjectURLWhere's revokeObjectURL?
new Worker, spawn, new Client per requestWhat's the limit?
setInterval at the top of a moduleDoes the process ever exit?
tmpdir() + Date.now()Unique? Deleted on failure?
server.close() not awaitedAre requests in flight dropped?

Go deeper