Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/js/internal/sql/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
SQLQueryFlags,
symbols: { _strings, _values },
} = require("internal/sql/query");
const { monotonicNowMs } = require("internal/timers");

declare global {
interface NumberConstructor {
Expand Down Expand Up @@ -609,9 +610,9 @@ abstract class BasePooledConnection<ConnectionHandle extends { close(): void; fl
flags: number = 0;
/// queryCount is used to indicate the number of queries using the connection, if a connection is reserved or if its a transaction queryCount will be 1 independently of the number of queries
queryCount: number = 0;
/// when the current connect cycle started; 0 when not connecting. Connect
/// failures (server not yet accepting connections) are retried until
/// connectionTimeout elapses from this point.
/// monotonicNowMs() when the current connect cycle started; 0 when not
/// connecting. Connect failures (server not yet accepting connections) are
/// retried until connectionTimeout elapses from this point.
Comment thread
robobun marked this conversation as resolved.
Resolved
connectStartedAt: number = 0;
connectAttempts: number = 0;
retryTimer: ReturnType<typeof setTimeout> | null = null;
Expand Down Expand Up @@ -641,7 +642,7 @@ abstract class BasePooledConnection<ConnectionHandle extends { close(): void; fl
async #beginConnecting() {
// a fresh connect cycle (not a backoff retry) starts the retry budget
if (this.connectStartedAt === 0) {
this.connectStartedAt = Date.now();
this.connectStartedAt = monotonicNowMs();
this.connectAttempts = 0;
}
await this.startConnection();
Expand Down Expand Up @@ -742,7 +743,7 @@ abstract class BasePooledConnection<ConnectionHandle extends { close(): void; fl
if (connectionTimeout <= 0) {
return false;
}
return this.connectStartedAt !== 0 && Date.now() - this.connectStartedAt < connectionTimeout;
return this.connectStartedAt !== 0 && monotonicNowMs() - this.connectStartedAt < connectionTimeout;
}

/// Returns true if a scheduled connect retry was cancelled; in that case
Expand Down
9 changes: 9 additions & 0 deletions src/js/internal/timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const NumberIsFinite = Number.isFinite;

const TIMEOUT_MAX = 2 ** 31 - 1;

/**
* Monotonic milliseconds for deadlines the runtime itself measures. Not
* `Date.now()` / `performance.now()`: bun:test's `setSystemTime()` and
* `useFakeTimers()` override both inside the engine (capturing the function
* does not help), which would freeze or skew an internal deadline.
*/
Comment thread
robobun marked this conversation as resolved.
Resolved
const monotonicNowMs = $newRustFunction("runtime/timer/Timer.rs", "internal_bindings.monotonicNowMs", 0);

function getTimerDuration(msecs, name) {
validateNumber(msecs, name);
if (msecs < 0 || !NumberIsFinite(msecs)) {
Expand All @@ -29,4 +37,5 @@ export default {
// tests that inspect socket[kTimeout].
kTimeout: Symbol.for("::buntimeout::"),
getTimerDuration,
monotonicNowMs,
};
14 changes: 14 additions & 0 deletions src/runtime/timer/Timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,18 @@ pub(crate) mod internal_bindings {
// `js_number(f64)` (i64 → f64 is lossless for the millisecond range).
Ok(JSValue::js_number(now as f64))
}

/// Monotonic milliseconds for deadlines Bun's own JS measures (e.g. the SQL
/// connect-retry budget). bun:test's `setSystemTime()` / `useFakeTimers()`
/// override `Date.now()` and `performance.now()` inside the engine, so a
/// deadline measured with either freezes or jumps along with the mocked
/// clock. This is the real clock the timer heap is drained against.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[bun_jsc::host_fn]
pub(crate) fn monotonic_now_ms(
_global_this: &JSGlobalObject,
_call_frame: &CallFrame,
) -> JsResult<JSValue> {
let now = Timespec::now(TimespecMockMode::ForceRealTime).ms();
Ok(JSValue::js_number(now as f64))
}
}
102 changes: 100 additions & 2 deletions test/js/sql/sql-connect-error-reporting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@
// Uses plain TCP servers / closed ports so the tests run without Docker.

import { SQL } from "bun";
import { expect, test } from "bun:test";
import { expect, setSystemTime, test } from "bun:test";
import type net from "node:net";
import { closedPort, listeningServer, pgAuthenticationOk, pgErrorResponse, pgReadyForQuery } from "./wire-frames";
import {
closedPort,
listeningServer,
mysqlHandshakeV10,
mysqlOkPacket,
mysqlReadPackets,
pgAuthenticationOk,
pgErrorResponse,
pgReadyForQuery,
} from "./wire-frames";

// connectionTimeout (seconds, fractional allowed) bounds the connect-retry
// budget; keep it short in tests that expect the failure to surface. Tests
Expand All @@ -48,6 +57,20 @@ function postgresAuthOkAndReady(socket: net.Socket) {
socket.write(Buffer.concat([pgAuthenticationOk(), pgReadyForQuery()]));
}

/** Minimal MySQL server: greet, then answer the HandshakeResponse with OK. */
function mysqlGreetAndAuthOk(socket: net.Socket) {
let buffered = Buffer.alloc(0);
let authenticated = false;
socket.write(mysqlHandshakeV10());
socket.on("data", chunk => {
buffered = mysqlReadPackets(Buffer.concat([buffered, chunk]), seq => {
if (authenticated) return;
authenticated = true;
socket.write(mysqlOkPacket(seq + 1));
});
});
}

test("postgres: connection refused is reported distinctly and fails fast", async () => {
const port = await closedPort();
const start = Date.now();
Expand Down Expand Up @@ -336,3 +359,78 @@ test("mysql: connectionTimeout: 0 disables connect retries", async () => {
server.close();
}
});

// The retry budget is measured on the monotonic clock, so bun:test's
// setSystemTime() (which pins Date.now() at the given instant) neither keeps
// the pool retrying past connectionTimeout nor ends the budget early.
const ONE_DAY_MS = 24 * 60 * 60 * 1000;

test("postgres: the retry budget elapses while Date.now() is pinned by setSystemTime()", async () => {
const { port, server } = await listeningServer(socket => socket.destroy());
setSystemTime(new Date("2020-01-01T00:00:00Z"));
try {
// with the budget measured via Date.now() this never rejects: every
// attempt sees 0ms elapsed and the pool redials forever
const err = await connectError(`postgres://postgres@127.0.0.1:${port}/postgres`, 0.25);
expect(err.code).toBe("ERR_POSTGRES_CONNECTION_FAILED");
} finally {
setSystemTime();
server.close();
}
});

test("postgres: setSystemTime() jumping ahead during a connect cycle does not end the retry budget", async () => {
let connections = 0;
const { port, server } = await listeningServer(socket => {
if (++connections === 1) {
// the pool recorded the start of this cycle before the jump; a Date.now()
// budget would now see a day of the default 30s budget gone and give up
setSystemTime(new Date(Date.now() + ONE_DAY_MS));
socket.destroy();
return;
}
socket.once("data", () => postgresAuthOkAndReady(socket));
});
const db = new SQL({ url: `postgres://postgres@127.0.0.1:${port}/postgres`, max: 1 });
try {
await db.connect();
expect(connections).toBe(2);
} finally {
setSystemTime();
await db.close({ timeout: 0 });
server.close();
}
});

test("mysql: the retry budget elapses while Date.now() is pinned by setSystemTime()", async () => {
const { port, server } = await listeningServer(socket => socket.destroy());
setSystemTime(new Date("2020-01-01T00:00:00Z"));
try {
const err = await connectError(`mysql://root@127.0.0.1:${port}/mysql`, 0.25);
expect(err.code).toBe("ERR_MYSQL_CONNECTION_FAILED");
} finally {
setSystemTime();
server.close();
}
});

test("mysql: setSystemTime() jumping ahead during a connect cycle does not end the retry budget", async () => {
let connections = 0;
const { port, server } = await listeningServer(socket => {
if (++connections === 1) {
setSystemTime(new Date(Date.now() + ONE_DAY_MS));
socket.destroy();
return;
}
mysqlGreetAndAuthOk(socket);
});
const db = new SQL({ url: `mysql://root@127.0.0.1:${port}/mysql`, max: 1 });
try {
await db.connect();
expect(connections).toBe(2);
} finally {
setSystemTime();
await db.close({ timeout: 0 });
server.close();
}
});