DEV Community

Cover image for Silent Failure #11: The Shop I Deleted Came Back, and the Resolver Still Mints Them
Renhua Wei
Renhua Wei

Posted on Edited on Originally published at dev.to

Silent Failure #11: The Shop I Deleted Came Back, and the Resolver Still Mints Them

I deleted the demo shop from my own platform page, restarted the app, and it was back: My Shop, id 1, empty orders.db, eight default workflow stages. I deleted it again, restarted, and it came back again. Two rounds of that and the joke writes itself — my product ships a resurrection feature nobody asked for.

The line responsible was the last one in init_db():

def init_db():
    init_registry()
    seed_templates(1, factory_db_path(1))
Enter fullscreen mode Exit fullscreen mode

Boot asks the resolver for the path to shop 1. The resolver, when it cannot find an id, inserts the row and creates the database. So a restart functioned as an upsert of whichever id boot happened to care about, and shop 1 was hardcoded in that path, which made deleting shop 1 impossible on any machine that ever rebooted.

I fixed the boot half and shipped it:

factory_ids = [r[0] for r in boot.execute('SELECT id FROM factories ORDER BY id').fetchall()]
for fid in factory_ids:
    seed_templates(fid, factory_db_path(fid))
Enter fullscreen mode Exit fullscreen mode

app.py +8/−3, test file +57 lines. The test deletes every shop except #3, calls init_db(), and asserts three things: the registry reads back [3], there is no orders.db on disk, and the fallback admin account lands on an existing shop instead of the one that was deleted. Suite runs 114 passed, 2 xfailed.

Then I closed the ticket, and that is the part I want to be honest about: I closed it because the repro stopped reproducing. The bug class was never about boot.

factory_db_path() still inserts. I measured it this morning against a fresh data dir instead of arguing with myself:

before: [1]
after : [1, 424242]
dbs   : ['factory_424242.db', 'orders.db', 'registry.db']
Enter fullscreen mode Exit fullscreen mode

One call, one id nobody ever created, and now there is a shop called Shop 424242, a database file for it, and a row in the registry. No HTTP request required — every caller that resolves an id can mint a tenant by accident. For a while I was convinced this was the same bug as the boot one and that the diff above had covered it. It had not; it covered the caller.

The caller was the noisy one. Boot resurrected a shop I could see, at a moment I could see it, once per restart. The resolver is quiet: it writes on behalf of whatever stale session, deleted operator, or typo'd id reaches it, and the write looks like a normal code path instead of a resurrection.

Silly as this is, there is a reason it survived review. The boot fix had a test with three assertions and a green suite behind it. A green test that proves one caller stopped calling is not evidence about the callee, and I read it as if it were. That is the same false green I keep writing about, this time with my own name on the commit.

Where the 0 problem sits, since someone will ask: three call sites still do int(user_id or 1) — get_db_file(), get_db() via session.get('user_id') or 1, and log_action(). So 0 and None both resolve to shop 1. I have read those lines, not exercised them end to end, so treat that one as a suspicion with line numbers rather than a measurement.

The change I have not shipped is the one that matters: split the resolver into a read that returns None and a create that INSERTs, then let the views 404. Until that lands, the fix above is a seatbelt on one door of a car with no doors.

My take, and I will defend it: any function that can INSERT must not be named ..._path or get_.... get_ and ensure_ are different verbs with different silent failure modes, and every merge of them turns a lookup into a write with a friendly name. I would rather 404 a shop that deserves a 200 than hand a stale session an empty tenant that shows up in my shop count.

I have measured this on one Flask app backed by SQLite, so scale my confidence accordingly.

The question I want a real answer to: in a Flask 3.x app where the current shop comes from session['user_id'], how many of your helpers named get_* or *_path are able to INSERT? Mine was one, and it outlived a green regression test.

If you think a resolver that creates on demand is a reasonable trade for shorter route code, I'd argue you are one stale session away from a shop you cannot explain — bring the case where it saved you, comments are open.

Top comments (4)

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

If the read-only version keeps fid = int(fid or 1) from the old one, there's a second leak hiding behind the first. Flask's <int:fid> converter accepts 0, and 0 or 1 is 1. I ran your split against a toy registry on Flask 3.1.3: /factory/424242 gives 404 now, but /factory/0 and /factory/00 both return 200 with tenant 1's data. That's your real first shop on orders.db. A typo now shows someone a real tenant instead of an empty one, which I'd say is worse. Dropping the or 1 and letting startup pass 1 explicitly closes it.

Collapse
 
dafili profile image
Renhua Wei •

You're right that int(fid or 1) survives — line 302, still there — but /factory/0 404s in my tree: no route takes a factory id from the path, which is why /factory/0, /factory/00 and /factory/424242 all return 404 on 3.1.3. The read-only version you tested never shipped: I had described a split I never wrote, and the post is corrected as of 04:40Z, so the honest state is that the shipped diff (app.py +8/−3) only stops boot from seeding a hardcoded id. The hole that is actually open is the resolver: factory_db_path(424242) mints Shop 424242 and factory_424242.db from an empty data dir, so any stale session['user_id'] is enough, and splitting it is the next change.

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

Fair, I tested a version that only existed in the text, my bad. For the resolver, one cheap guard while you split it: open the tenant file with sqlite3.connect(f"file:{path}?mode=rw", uri=True). On a missing file that raises "unable to open database file" and creates nothing, while a plain connect quietly makes factory_424242.db. So a stale session gets an error instead of a fresh shop.

Thread Thread
 
dafili profile image
Renhua Wei •

Took the guard, it's cheaper than the check I had. Confirmed on 3.46.1 with your exact URI: a missing file raises OperationalError "unable to open database file", same error for a path that is a directory, and nothing is created, while the plain connect had 8192 bytes on disk right after the first create table. One catch: mode=rw also refuses a read-only mount, so map that failure to a 5xx instead of "unknown tenant" or a full disk starts looking like a routing bug.