#16524·mongoose

Model compiled while readyState reports a stale heartbeat never initializes: Model.init() stays pending and autoIndex never runs (9.10.2)

Author: omprashantjainCreated Sep 24, 2026Updated Sep 24, 2026

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

9.10.2 (driver 7.6.0)

Node.js version

v24.18.0

MongoDB server version

8.2.6 (mongodb-memory-server, single-node replica set)

Description

Since #16183 (fixed in 9.4.0), connection-level buffered operations are flushed when a heartbeat arrives after the readyState getter has reported a stale connection. Collections created during that stale window are not covered by that fix.

If a model is compiled while the getter reports disconnected because the last heartbeat is older than 2 * heartbeatFrequencyMS (for example after the event loop was blocked), Collection never calls onOpen(). The internal _readyState never left connected, so no state transition happens later, and the heartbeat recovery path only flushes conn._queue. As a result:

  • automatic ensureIndexes is queued with collection.addQueue(create) (model.js, !model.collection.collection) and never runs;
  • Model.init() / model.$init never settles.

Ordinary operations (create, find) still work through _getCollection().

Steps to reproduce

javascript
import mongoose from "mongoose";
import { MongoMemoryReplSet } from "mongodb-memory-server";

const replSet = await MongoMemoryReplSet.create({ replSet: { count: 1 } });
await mongoose.connect(replSet.getUri(), { heartbeatFrequencyMS: 500 });
await new Promise((resolve) => setTimeout(resolve, 3000)); // let heartbeats start

// Block the event loop for longer than 2 * heartbeatFrequencyMS.
const until = Date.now() + 1500;
while (Date.now() < until) {}
console.log("readyState right after the stall:", mongoose.connection.readyState); // 0

const Thing = mongoose.model("Thing", new mongoose.Schema({ name: { type: String, unique: true } }));

await new Promise((resolve) => setTimeout(resolve, 2000)); // several heartbeats later
console.log("readyState after heartbeats resumed:", mongoose.connection.readyState); // 1

await Thing.create({ name: "a" }); // works
console.log("Thing.init():", await Promise.race([
  Thing.init().then(() => "settled"),
  new Promise((resolve) => setTimeout(() => resolve("still pending after 10 s"), 10_000)),
]));
console.log("indexes:", (await Thing.collection.indexes()).map((index) => index.name));

await mongoose.disconnect();
await replSet.stop();

Output (3 of 3 runs):

readyState right after the stall: 0
readyState after heartbeats resumed: 1
Thing.init(): still pending after 10 s
indexes: [ '_id_' ]

Without the busy loop, the same script prints Thing.init(): settled and indexes: [ '_id_', 'name_1' ].

Expected behavior

When heartbeats resume, collections created during the stale window should be opened (or should not have skipped onOpen() in the first place, since the underlying connection never closed). Model.init() should then settle, and automatic index builds should run.

Related: #16183.