Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error:Kill child process will case "kill ESRCH" when "Error in connection establishment: net::ERR_CONNECTION_REFUSED" #8534

Closed
zhaomenghuan opened this issue Sep 21, 2020 · 5 comments · Fixed by #8661 or #8809
Labels
bug bugs found in the application electron issues related to the electron target

Comments

@zhaomenghuan
Copy link
Contributor

Bug Description:

Two problems that can be reproduced stably.

  • Error in connection establishment: net::ERR_CONNECTION_REFUSED
  • Kill ESRCH

Steps to Reproduce:

When I opened the Electron desktop application built on Theia, and did not operate for a period of time, when I opened DevTools, I could see the following error:
屏幕快照 2020-09-21 下午2 09 09
I click the exit button:
屏幕快照 2020-09-21 下午2 08 48
Error popup will pop up:
屏幕快照 2020-09-21 下午2 08 31
The code that caused the error is:

process.kill(backendProcess.pid);

Additional Information

  • Operating System: macOS
  • Theia Version:
@vince-fugnitto
Copy link
Member

@zhaomenghuan I believe you may have encountered the following issue: #4249

@vince-fugnitto vince-fugnitto added bug bugs found in the application electron issues related to the electron target labels Sep 21, 2020
@kittaakos
Copy link
Contributor

I have noticed this issue several times already; for some reason, the backed process just terminates. When closing the electron app, we try to close the forked BE process, but it's not there anymore. This issue requires two fixes:

@zhaomenghuan
Copy link
Contributor Author

Is there any better solution to handle the failed "Error in connection establishment: net:ERR_CONNECTION_REFUSED" exception? When is the official plan to fix this problem? The application experience of this problem in the Electron environment is very affected.

@kittaakos
Copy link
Contributor

"Error in connection establishment: net:ERR_CONNECTION_REFUSED" exception?

For the record, this 👆 exception is causing the Kill ESRCH problem. You have to figure out why does the backend process crash and terminate on your side. Maybe, it's a generic Theia issue.

As for the Kill ERSCH error dialog, you can customize your electron application like this to ignore the ESRCH:

your-electron-main-module.ts:

export default new ContainerModule((bind, unbind, isBound, rebind) => {
    bind(YourElectronMainApplication).toSelf().inSingletonScope();
    rebind(ElectronMainApplication).toService(ElectronMainApplication);
});

your-electron-main-application.ts:

@injectable()
export class YournElectronMainApplication extends ElectronMainApplication {

    protected async startBackend(): Promise<number> {
        // Check if we should run everything as one process.
        const noBackendFork = process.argv.indexOf('--no-cluster') !== -1;
        // We cannot use the `process.cwd()` as the application project path (the location of the `package.json` in other words)
        // in a bundled electron application because it depends on the way we start it. For instance, on OS X, these are a differences:
        // https://github.com/eclipse-theia/theia/issues/3297#issuecomment-439172274
        process.env.THEIA_APP_PROJECT_PATH = this.globals.THEIA_APP_PROJECT_PATH;
        // Set the electron version for both the dev and the production mode. (https://github.com/eclipse-theia/theia/issues/3254)
        // Otherwise, the forked backend processes will not know that they're serving the electron frontend.
        process.env.THEIA_ELECTRON_VERSION = process.versions.electron;
        if (noBackendFork) {
            process.env[ElectronSecurityToken] = JSON.stringify(this.electronSecurityToken);
            // The backend server main file is supposed to export a promise resolving with the port used by the http(s) server.
            const address: AddressInfo = await require(this.globals.THEIA_BACKEND_MAIN_PATH);
            return address.port;
        } else {
            let args = this.processArgv.getProcessArgvWithoutBin();
            // https://github.com/eclipse-theia/theia/issues/8227
            if (process.platform === 'darwin') {
                // https://github.com/electron/electron/issues/3657
                // https://stackoverflow.com/questions/10242115/os-x-strange-psn-command-line-parameter-when-launched-from-finder#comment102377986_10242200
                // macOS appends an extra `-psn_0_someNumber` arg if a file is opened from Finder after downloading from the Internet.
                // "AppName" is an app downloaded from the Internet. Are you sure you want to open it?
                args = args.filter(arg => !arg.startsWith('-psn'));
            }
            const backendProcess = fork(
                this.globals.THEIA_BACKEND_MAIN_PATH,
                args,
                await this.getForkOptions(),
            );
            return new Promise((resolve, reject) => {
                // The backend server main file is also supposed to send the resolved http(s) server port via IPC.
                backendProcess.on('message', (address: AddressInfo) => {
                    resolve(address.port);
                });
                backendProcess.on('error', error => {
                    reject(error);
                });
                app.on('quit', () => {
                    try {
                        // If we forked the process for the clusters, we need to manually terminate it.
                        // See: https://github.com/eclipse-theia/theia/issues/835
                        process.kill(backendProcess.pid);
                    } catch (e) {
                        if (e.code === 'ESRCH') {
                            console.log('Could not terminate the backend process. It was not running.');
                            return;
                        }
                        throw e;
                    }
                });
            });
        }
    }

}

Or provide us a patch with the fix.

@zhaomenghuan
Copy link
Contributor Author

zhaomenghuan commented Oct 22, 2020

When I hibernate my computer and turn it on again, the problem can be reproduced stably. I added the close and exit events of backendProcess to ElectronMainApplication, and found that the signal values are both SIGPIPE. Node.js Process ignores SIGPIPE errors by default, but it doesn't seem to take effect in the Electron environment. I tried adding the following code to BackendApplication, which seems to solve the problem.

@injectable()
export class BackendApplication {
    ...
    constructor(
        @inject(ContributionProvider) @named(BackendApplicationContribution)
        protected readonly contributionsProvider: ContributionProvider<BackendApplicationContribution>,
        @inject(BackendApplicationCliContribution) protected readonly cliParams: BackendApplicationCliContribution
    ) {
        ...
        process.on('SIGPIPE', () => {
            console.log("No, please do not exit...");
        });
        ...
    }
    ...
}
...

electron issues reference:electron/electron#13254 (comment)
vscode code reference:https://github.com/microsoft/vscode/blob/master/src/bootstrap.js#L34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug bugs found in the application electron issues related to the electron target
Projects
None yet
3 participants