Skip to content

Admin blocks (and times out) on long running unawaited tasks #4768

@ssyberg

Description

@ssyberg

Link to reproduction

No response

Describe the Bug

Apologies if this is expected behavior, my grasp of sync/async node is not stellar but this does smell a bit funny to me. I'm finding that if an afterChange hook does a lot of long running nonblocking work (in my case it's a few thousand record updates), it still blocks admin completing and generally leads to a timeout.

To Reproduce

Create the below collection and save a record, you'll see that the record itself is immediately created but then admin is stuck on the "loading" screen indefinitely and eventually times out.

const Sandbox = {
    slug: 'sandbox',
    fields: [
        {
            name: 'name',
            type: 'text',
            hooks: {
                afterChange: [
                    async (args) => {
                        console.log(`Starting after change hook`);

                        // Do 2000 payload writes but don't await them
                        for (let i = 0; i < 2000; i++) {
                            args.req.payload.create({
                                collection: 'sandbox',
                                data: { name: `name ${Math.random()}` },
                            });
                        }

                        console.log(`Ending after change hook`);
                    },
                ],
            },
        },
    ],
};

export default Sandbox;

Payload Version

2.7.0

Adapters and Plugins

db-mongodb

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs-triagePossible bug which hasn't been reproduced yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions