Skip to content

Commit b2dabc4

Browse files
kckernclaude
andcommitted
fix(web): retry a search once when searchd closed the connection
Staging parity hit a 500 on /search/faith.hope.charity: the Sphinx client reused a connection searchd had closed, and mysql2 failed the query with a fatal "closed state" error. Searches are read-only, so retry once on a fresh connection. Also allow the ui.css #logout removal (decision #11). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3f205fb commit b2dabc4

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎apps/web/src/data/external/sphinx.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ export class SphinxClient {
4646
return this.conn;
4747
}
4848

49-
async query(sql) {
49+
async query(sql, retry = true) {
5050
const conn = await this.connection();
5151
try {
5252
const [rows] = await conn.query(sql);
5353
return rows;
5454
} catch (err) {
5555
// A dropped connection is replaced on the next call
56-
this.conn = null;
56+
if (this.conn && (await this.conn.catch(() => null)) === conn) this.conn = null;
5757
try { await conn.end(); } catch { /* already closed */ }
58+
// mysql2 marks socket-level failures fatal (searchd restart, idle timeout). Searches are
59+
// read-only, so retry once on a fresh connection instead of failing the request.
60+
if (retry && err.fatal) return this.query(sql, false);
5861
throw err;
5962
}
6063
}

‎apps/web/test/parity/allow.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"html re:^/([A-Za-z0-9*.]+/)?(\\d+|[1-4]?[A-Za-z&]+\\.?\\d+([.:]\\d+)+)[a-z]$": "Subverse letter on a reference-shaped path (book.chapter.verse or a verse id, then one letter: john.3.16a, kjv/1ne.3.7b, fax/1879l): PHP's /\\d[a-z]$/ drops the digit with the letter (john.3.16a is John 3:1); Node keeps the verse — owner decision not to reproduce",
1111
"html re:^/([A-Za-z0-9*.]+/)?(?![Gg][Ee][Nn])[A-Za-z]+(-[A-Za-z]+)+\\d+$ && live:<title>Genesis 1–50Exodus ": "Live expands unbounded multi-book ranges from junk URLs; Node caps the range to protect the server (controller decision 2026-10-20). Only paths shaped /[<version>/]<word>-<word><digit> whose first word is not Genesis, and only when live's title lists every book from Genesis 1 on: the unresolved start PHP read as verse 1 (about 2 MB). Node expands at most 1000 verses (the package's own limit) and shows the block as not found",
1212
"asset /js/main.js": "Google sign-in removed: dead on live (owner decision #11, 2026-10-22)",
13+
"asset /css/ui.css": "Google sign-in removed: the settings panel's Logout button and its #logout rule are gone (owner decision #11, 2026-10-22)",
1314
"* check:staleSectionHeading": "Live heads a passage whose first verse starts a section with § but not §¶ (a psalm superscription, NIV Ps. 23:1 \"§A psalm of David.¶▼…\", or after a division label, NET Ps. 90:1 \"Book 4 (Psalms 90-106)§A prayer…\") with the section before it (\"Psalm 22\" and an ellipsis on /niv/ps.23); Node heads it with the section at that verse, and a whole chapter that is one section gets section nav as with §¶; a label before §¶ (NIV Ps. 1:1 \"§Psalms 1–41§¶…\") keeps its passage without live's heading (owner decision 2026-10-22). Allowed only where live shows that heading and ellipsis ending right before the first verse, the first verse's live raw text starts a section, Node's heading is the raw heading with the section's range on live's tag, and nothing else differs but the nav labels of the same links",
1415
"latex re:(^|[/;])jst\\. && check:latexMissingVerses": "PHP latex read LDS only and dropped JST verses; Node renders each block's version (decision 2026-10-23). Only for references with a JST piece, and only where every live verse is in Node's output and Node has more (live empty or missing verses)"
1516
}

‎apps/web/test/unit/sphinx.test.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ describe('SphinxClient', () => {
5353
expect(connect).toHaveBeenCalledTimes(2);
5454
});
5555

56+
test('retries once on a fresh connection when the old one was closed under it', async () => {
57+
// searchd restarts or idle timeouts close the socket; mysql2 then fails the next query with a
58+
// fatal "closed state" error. Searches are read-only, so one retry is safe.
59+
const closed = Object.assign(new Error("Can't add new command when connection is in closed state"), { fatal: true });
60+
let calls = 0;
61+
const connect = vi.fn(async () => ({
62+
query: vi.fn(async () => { if (calls++ === 0) throw closed; return [[{ verse_id: '1', version: 'LDS' }]]; }),
63+
end: vi.fn(async () => {}),
64+
}));
65+
const client = new SphinxClient({ host: 'sphinx' }, connect);
66+
await expect(client.search('faith')).resolves.toEqual([{ verse_id: 1, version: 'LDS' }]);
67+
expect(connect).toHaveBeenCalledTimes(2);
68+
});
69+
70+
test('does not retry a fatal error twice', async () => {
71+
const closed = Object.assign(new Error('closed'), { fatal: true });
72+
const connect = vi.fn(async () => ({ query: vi.fn(async () => { throw closed; }), end: vi.fn(async () => {}) }));
73+
const client = new SphinxClient({ host: 'sphinx' }, connect);
74+
await expect(client.query('SELECT 1')).rejects.toThrow('closed');
75+
expect(connect).toHaveBeenCalledTimes(2);
76+
});
77+
5678
test('createSphinxClient is null without a host', () => {
5779
expect(createSphinxClient({ host: '' })).toBeNull();
5880
expect(createSphinxClient({ host: 'sphinx', port: 9306 })).toBeInstanceOf(SphinxClient);

0 commit comments

Comments
 (0)