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

fix: Use message window for requestSingleInstanceLock #34279

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions shell/browser/api/electron_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,9 @@ void App::OnFirstInstanceAck(
}
}
Emit("first-instance-ack", data_to_send);

// Reset the singleton
process_singleton_.reset();
}

// This function handles the user calling
Expand Down Expand Up @@ -1174,6 +1177,8 @@ bool App::RequestSingleInstanceLock(gin::Arguments* args) {
process_singleton_.reset();
return false;
}
case ProcessSingleton::NotifyResult::PROCESS_NOTIFIED_AWAITING_ACK:
return false; // We reset the singleton after receiving the ack.
case ProcessSingleton::NotifyResult::PROCESS_NONE:
default: // Shouldn't be needed, but VS warns if it is not there.
return true;
Expand Down
23 changes: 18 additions & 5 deletions spec-main/api-app-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ describe('app module', () => {
expect(code).to.equal(0);
});

it('allows two different user data dirs to hold two different instances', async function () {
const appPath = path.join(fixturesPath, 'api', 'singleton-userdata-double');
const instance = cp.spawn(process.execPath, [appPath, '--user-data-dir-suffix=first']);
const firstInstanceExit = emittedOnce(instance, 'exit');
const anotherInstance = cp.spawn(process.execPath, [appPath, '--user-data-dir-suffix=second']);
const secondInstanceExit = emittedOnce(anotherInstance, 'exit');
const [code] = await firstInstanceExit;
const [secondCode] = await secondInstanceExit;
expect(code).to.equal(0);
expect(secondCode).to.equal(0);
});

async function testArgumentPassing (testArgs: SingleInstanceLockTestArgs) {
const appPath = path.join(fixturesPath, 'api', 'singleton-data');
const first = cp.spawn(process.execPath, [appPath, ...testArgs.args]);
Expand All @@ -247,14 +259,15 @@ describe('app module', () => {
}
const additionalDataPromise = emittedOnce(firstStdoutLines, 'data');

const secondInstanceArgs = [process.execPath, appPath, ...testArgs.args, '--some-switch', 'some-arg'];
const secondInstanceArgs = [process.execPath, appPath, ...testArgs.args, '--some-switch=some-arg'];
const second = cp.spawn(secondInstanceArgs[0], secondInstanceArgs.slice(1));
const secondExited = emittedOnce(second, 'exit');
const secondStdoutLines = second.stdout.pipe(split());
let ackData;
while ((ackData = await emittedOnce(secondStdoutLines, 'data'))[0].toString().length === 0) {
// This isn't valid data.
let ackData = (await emittedOnce(secondStdoutLines, 'data')).toString();
while (ackData === 'started' || ackData.trim().length === 0) {
ackData = (await emittedOnce(secondStdoutLines, 'data')).toString();
}
// const ackDataPromise = emittedOnce(secondStdoutLines, 'data');

const [code2] = await secondExited;
expect(code2).to.equal(1);
Expand All @@ -264,7 +277,7 @@ describe('app module', () => {
const [args, additionalData] = dataFromSecondInstance[0].toString('ascii').split('||');
const secondInstanceArgsReceived: string[] = JSON.parse(args.toString('ascii'));
const secondInstanceDataReceived = JSON.parse(additionalData.toString('ascii'));
const dataAckReceived = JSON.parse(ackData[0].toString('ascii'));
const dataAckReceived = JSON.parse(ackData);

// Ensure secondInstanceArgs is a subset of secondInstanceArgsReceived
for (const arg of secondInstanceArgs) {
Expand Down
15 changes: 8 additions & 7 deletions spec/fixtures/api/singleton-data/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ if (app.commandLine.hasSwitch('ack-content')) {

app.on('first-instance-ack', (event, additionalData) => {
console.log(JSON.stringify(additionalData));
setImmediate(() => {
app.exit(1);
});
});

const gotTheLock = sendAdditionalData
Expand All @@ -56,13 +59,11 @@ app.on('second-instance', (event, args, workingDirectory, data, ackCallback) =>
}
setImmediate(() => {
console.log([JSON.stringify(args), JSON.stringify(data)].join('||'));
sendAck ? ackCallback(ackObj) : ackCallback();
setImmediate(() => {
if (preventDefault) {
sendAck ? ackCallback(ackObj) : ackCallback();
}
setTimeout(() => {
app.exit(0);
});
}, 500);
});
});

if (!gotTheLock) {
app.exit(1);
}
20 changes: 20 additions & 0 deletions spec/fixtures/api/singleton-userdata-double/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { app } = require('electron');
const fs = require('fs');
const path = require('path');

const hasSuffix = app.commandLine.hasSwitch('user-data-dir-suffix');

if (!hasSuffix) {
app.exit(2);
}

const userDataDirSuffix = app.commandLine.getSwitchValue('user-data-dir-suffix');

const userDataFolder = path.join(app.getPath('home'), 'electron-test-singleton-userdata-double-' + userDataDirSuffix);
app.setPath('userData', userDataFolder);

const gotTheLock = app.requestSingleInstanceLock();

setTimeout(() => {
app.exit(gotTheLock ? 0 : 1);
}, 500);
4 changes: 4 additions & 0 deletions spec/fixtures/api/singleton-userdata-double/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "electron-test-singleton-userdata",
"main": "main.js"
}
11 changes: 7 additions & 4 deletions spec/fixtures/api/singleton/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ app.whenReady().then(() => {
console.log('started'); // ping parent
});

app.on('first-instance-ack', (event, additionalData) => {
console.log(JSON.stringify(additionalData));
setImmediate(() => {
app.exit(1);
});
});

const gotTheLock = app.requestSingleInstanceLock();

app.on('second-instance', (event, args, workingDirectory) => {
Expand All @@ -12,7 +19,3 @@ app.on('second-instance', (event, args, workingDirectory) => {
app.exit(0);
});
});

if (!gotTheLock) {
app.exit(1);
}