Skip to content

Failed development config reload leaves concurrent and later getPayload calls pending forever #19244

Open

Description

@cbratschi

Description

When a development config reload fails, the initiating getPayload() call
rejects, but the promise stored in cached.reload is never settled. Concurrent
and subsequent calls for the same instance await that promise indefinitely.

This happens when options.config rejects or when reload() throws, for example
during a database reconnect. This report concerns the missing error path after
a reload has started; it does not establish why an underlying reconnect failed.

Affected source

The cached.reload === true branch in packages/payload/src/index.ts stores a
manually resolved promise before awaiting config and reload. Both resolve()
and cached.reload = false occur only on success.

Reproduced with Payload 3.92.1 and Node.js 26.12.1. The same resolve-only branch
is present on inspected main commit
ec351643011603cf75dc91db5d03f80ff120f1e9.

Related PR #19054
addresses changes arriving during reload. Its description explicitly lists
reload() throwing and leaving the promise pending as a separate, unfixed issue.
This report does not duplicate that queued-change work.

Minimal reproduction

Save as repro.mjs in a project with Payload 3.92.1 installed and run:

NODE_ENV=test node repro.mjs

The script seeds only its own isolated cache key to enter the already-initialized
reload branch. It uses the actual installed getPayload implementation, loads
no project config, opens no database connection and removes only its own key.
Run it in a fresh Node process, not inside a running application.

import assert from 'node:assert/strict';
import { setImmediate } from 'node:timers/promises';
import { getPayload } from 'payload';

const key = 'isolated-config-rejection';
const cache = globalThis._payload;
const states = [];
const failure = new Error('Controlled config rejection');
const cached = {
    payload: {},
    promise: null,
    reload: true,
    initializedCrons: true
};
const observe = promise => {
    const index = states.push('pending') - 1;

    return promise.then(() => {
        states[index] = 'fulfilled';
    }, error => {
        assert.equal(error, failure);
        states[index] = 'rejected';
    });
};

assert.equal(cache.has(key), false);
cache.set(key, cached);

try {
    const options = {
        config: Promise.reject(failure),
        key
    };
    const first = observe(getPayload(options));

    observe(getPayload(options));
    await first;
    await setImmediate();

    observe(getPayload({
        config: Promise.resolve({}),
        key
    }));
    await setImmediate();

    console.log(states);
    assert.deepEqual(states, ['rejected', 'pending', 'pending']);
    assert.ok(cached.reload instanceof Promise);
} finally {
    cache.delete(key);
}

Observed: ['rejected', 'pending', 'pending'] for the initiating, concurrent and
later call. An unrelated successful reload retains its ordinary behavior.

Expected: all calls awaiting a failed reload reject with the original error;
a subsequent call can retry the reload rather than waiting on an orphaned
promise. Existing successful calls must still share one reload operation.

Proposed correction and regression coverage

Store the real reload promise, including config resolution, and await that same
promise in every caller. On success, retain the existing successful state. On
failure, mark the reload retryable and rethrow the original error. Do not reset
application caches, restart processes, disable HMR or change production caching.

A local source-only candidate has been checked against the actual installed
function with rejected config and rejected reload stages: both waiting callers
reject with the identical error, the next call reloads successfully, and normal
parallel reloads still coalesce. Dependencies were not modified.

The queued-change behavior in #19054 is separate; if that PR lands first, the
error-path correction must preserve its queue and replaced-config handling.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions