Release the Connection! — JavaScript Bug Hunt

Inspired by the slow-motion outage every service hits eventually: a query throws, the error path skips release(), and the pool bleeds one connection per…

  • Language: JavaScript
  • Layer: Database
  • Difficulty: Medium
  • Concepts: Connection Pool, Error Handling
  • Modelled on: Connection pools
  • Visible tests: the happy path acquires, runs and releases; a throwing query still releases
  • Reward: 50 XP for a complete fix

Briefing

Inspired by the slow-motion outage every service hits eventually: a query throws, the error path skips release(), and the pool bleeds one connection per failure until nothing is left at 3 AM.

The pool is locked. withConnection must release on every path.

Bug report

BUG-POOL-DRAIN · Priority: Critical · Reported by: 3 AM pager

withConnection(pool, fn):

  • acquires, runs fn(conn), returns its result
  • the connection is released whether fn returns OR throws
  • the original error still propagates

Observed: pool.available() ratchets down after every failed query, then "pool exhausted" takes the whole service down.

Logs

[pool] available: 10 -> 9 -> 8 -> ... -> 0
[api] Error: pool exhausted

The code as shipped

src/db/withConnection.js (editable)

var pool = require("./pool");

exports.withConnection = function (fn) {
  var conn = pool.acquire();
  var result = fn(conn);
  pool.release();
  return result;
};

Read-only context: src/db/pool.js.

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More JavaScript bug hunts.