简体   繁体   English

不可能的错误……并发问题? NextJs / 节点 HTTP 服务器

[英]Impossible error… concurrency issue? NextJs / Node HTTP Server

I am having a problem that is supposed not to happen:我遇到了一个不应该发生的问题:

在此处输入图像描述

See initialUserState is being set depending on the result of a ternary operation.请参阅根据三元运算的结果设置的initialUserState However, this operation should return false here (as there's no user property in context object. (You can see it in the debugger below)但是,此操作应在此处返回 false(因为context object 中没有user属性。(您可以在下面的调试器中看到它)

What could be possibly going on here?这里可能发生了什么?

The data is from a previous request in a NextJS application (I am using Firefox and Firefox Private mode).数据来自 NextJS 应用程序中的先前请求(我使用的是 Firefox 和 Firefox 私有模式)。 This only happens when the request are quick next to each other.这仅在请求彼此快速相邻时才会发生。 So I think this is a concurrency issue, but can't understand why javascript would fail with that condition.所以我认为这是一个并发问题,但不明白为什么 javascript 在这种情况下会失败。 How initialUserState is holding that value if I am declaring it again here.如果我在这里再次声明它, initialUserState是如何保持该值的。

import {userInitialState} from "./UserContext";
import {listInitialState} from "./ListContext";
import {createContext, useState} from "react";

/**
 * Sets the initial application context
 */
export default class ApplicationContext {

    userContext;
    listContext;

    /**
     * Will set the initial application state.
     * @param context
     */
    constructor(context) {
        let initialUserState = context.hasOwnProperty('user') ? context.user : userInitialState;
        const userContextUpdater = () => {
            let [state, setState] = useState({...initialUserState})
            return [state, setState];
        }
        this.userContext = createContext(userContextUpdater);

        let initialListState = context.hasOwnProperty('list') ? context.list : listInitialState;
        const listContextUpdater = () => {
            let [state, setState] = useState({...initialListState})
            return [state, setState];
        }
        this.listContext = createContext(listContextUpdater);
    }

    /**
     * Returns the UserContext
     * @returns {*}
     */
    getUserContext = () => {
        return this.userContext;
    }

    /**
     * Returns the List context
     * @returns {*}
     */
    getListContext = () => {
        return this.listContext;
    }
}

_app.js _app.js

import ApplicationContext from "../lib/Context/ApplicationContext";
import {useContext} from "react";

export default function RankerApp({Component, pageProps}) {
    const applicationContext = new ApplicationContext(pageProps);
    const ListContext = applicationContext.getListContext();
    const UserContext = applicationContext.getUserContext();

    const [listState, setListState] = useContext(ListContext)();
    const [userState, setUserState] = useContext(UserContext)();

    return (
        <ListContext.Provider value={[listState, setListState]}>
            <UserContext.Provider value={[userState, setUserState]}>
                <Component {...pageProps} applicationContext={applicationContext}/>
            </UserContext.Provider>
        </ListContext.Provider>
    );
}

UserContext.js UserContext.js

export const userInitialState = {
    currentUserId: undefined,
    users: {},
}

This is driving me nuts, it looks like it is some kind of concurrency issue, I can't explain it otherwise, as this doesn't happen if the request are spaced in time这让我发疯,看起来这是某种并发问题,我无法解释它,因为如果请求及时间隔,这不会发生

在此处输入图像描述

在此处输入图像描述

It seems I got bitten by object references.看来我被 object 引用咬了。

This was before:这是以前:

在此处输入图像描述

This is the fix这是修复

在此处输入图像描述

So here I assigned to the returnContext['user'] a reference to userInitialState所以在这里我为returnContext['user']分配了对userInitialState的引用

And then modified it right below.然后在下面修改它。

Since for some reason my NextJS app is persisting things (I don't know if this is because of being a compiled server, but the variables inside Pages remain if I put a counter), this value was also persisting.由于出于某种原因,我的 NextJS 应用程序正在持久化一些东西(我不知道这是否是因为它是一个已编译的服务器,但如果我放置一个计数器,Pages 中的变量仍然存在),这个值也保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM