Skip to content

Commit 67c29e8

Browse files
authored
chore: add missing await to floating promises (#5813)
1 parent be9fa74 commit 67c29e8

File tree

17 files changed

+46
-43
lines changed

17 files changed

+46
-43
lines changed

src/cli/cli.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ program
3939

4040
commandWithOpenOptions('open [url]', 'open page in browser specified via -b, --browser', [])
4141
.action(function(url, command) {
42-
open(command, url, language());
42+
open(command, url, language()).catch(logErrorAndExit);
4343
})
4444
.on('--help', function() {
4545
console.log('');
@@ -54,7 +54,7 @@ commandWithOpenOptions('codegen [url]', 'open page and generate code for user ac
5454
['-o, --output <file name>', 'saves the generated script to a file'],
5555
['--target <language>', `language to use, one of javascript, python, python-async, csharp`, language()],
5656
]).action(function(url, command) {
57-
codegen(command, url, command.target, command.output);
57+
codegen(command, url, command.target, command.output).catch(logErrorAndExit);
5858
}).on('--help', function() {
5959
console.log('');
6060
console.log('Examples:');
@@ -122,7 +122,7 @@ const browsers = [
122122
for (const {alias, name, type} of browsers) {
123123
commandWithOpenOptions(`${alias} [url]`, `open page in ${name}`, [])
124124
.action(function(url, command) {
125-
open({ ...command, browser: type }, url, command.target);
125+
open({ ...command, browser: type }, url, command.target).catch(logErrorAndExit);
126126
}).on('--help', function() {
127127
console.log('');
128128
console.log('Examples:');
@@ -137,7 +137,7 @@ commandWithOpenOptions('screenshot <url> <filename>', 'capture a page screenshot
137137
['--wait-for-timeout <timeout>', 'wait for timeout in milliseconds before taking a screenshot'],
138138
['--full-page', 'whether to take a full page screenshot (entire scrollable area)'],
139139
]).action(function(url, filename, command) {
140-
screenshot(command, command, url, filename);
140+
screenshot(command, command, url, filename).catch(logErrorAndExit);
141141
}).on('--help', function() {
142142
console.log('');
143143
console.log('Examples:');
@@ -150,7 +150,7 @@ commandWithOpenOptions('pdf <url> <filename>', 'save page as pdf',
150150
['--wait-for-selector <selector>', 'wait for given selector before saving as pdf'],
151151
['--wait-for-timeout <timeout>', 'wait for given timeout in milliseconds before saving as pdf'],
152152
]).action(function(url, filename, command) {
153-
pdf(command, command, url, filename);
153+
pdf(command, command, url, filename).catch(logErrorAndExit);
154154
}).on('--help', function() {
155155
console.log('');
156156
console.log('Examples:');
@@ -164,7 +164,7 @@ if (process.env.PWTRACE) {
164164
.option('--resources <dir>', 'load resources from shared folder')
165165
.description('Show trace viewer')
166166
.action(function(trace, command) {
167-
showTraceViewer(trace, command.resources);
167+
showTraceViewer(trace, command.resources).catch(logErrorAndExit);
168168
}).on('--help', function() {
169169
console.log('');
170170
console.log('Examples:');
@@ -179,7 +179,7 @@ if (process.argv[2] === 'run-driver')
179179
else if (process.argv[2] === 'print-api-json')
180180
printApiJson();
181181
else if (process.argv[2] === 'launch-server')
182-
launchBrowserServer(process.argv[3], process.argv[4]);
182+
launchBrowserServer(process.argv[3], process.argv[4]).catch(logErrorAndExit);
183183
else
184184
program.parse(process.argv);
185185

@@ -446,6 +446,11 @@ function validateOptions(options: Options) {
446446
}
447447
}
448448

449+
function logErrorAndExit(e: Error) {
450+
console.error(e);
451+
process.exit(1);
452+
}
453+
449454
function language(): string {
450455
return process.env.PW_CLI_TARGET_LANG || 'javascript';
451456
}

src/client/browserContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
8787
const func = this._bindings.get(bindingCall._initializer.name);
8888
if (!func)
8989
return;
90-
bindingCall.call(func);
90+
await bindingCall.call(func);
9191
}
9292

9393
setDefaultNavigationTimeout(timeout: number) {
@@ -238,7 +238,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
238238
});
239239
}
240240

241-
async _onClose() {
241+
_onClose() {
242242
if (this._browser)
243243
this._browser._contexts.delete(this);
244244
this.emit(Events.BrowserContext.Close, this);

src/client/page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
186186
async _onBinding(bindingCall: BindingCall) {
187187
const func = this._bindings.get(bindingCall._initializer.name);
188188
if (func) {
189-
bindingCall.call(func);
189+
await bindingCall.call(func);
190190
return;
191191
}
192-
this._browserContext._onBinding(bindingCall);
192+
await this._browserContext._onBinding(bindingCall);
193193
}
194194

195195
_onWorker(worker: Worker): void {

src/dispatchers/androidDispatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export class AndroidSocketDispatcher extends Dispatcher<SocketBackend, channels.
184184
}
185185

186186
async close(params: channels.AndroidSocketCloseParams, metadata: CallMetadata): Promise<void> {
187-
await this._object.close();
187+
this._object.close();
188188
}
189189
}
190190

src/remote/playwrightServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ export class PlaywrightServer {
4848
ws.on('message', message => dispatcherConnection.dispatch(JSON.parse(message.toString())));
4949
ws.on('close', () => {
5050
debugLog('Client closed');
51-
this._onDisconnect();
51+
this._onDisconnect().catch(debugLog);
5252
});
5353
ws.on('error', error => {
5454
debugLog('Client error ' + error);
55-
this._onDisconnect();
55+
this._onDisconnect().catch(debugLog);
5656
});
5757
dispatcherConnection.onmessage = message => ws.send(JSON.stringify(message));
5858
new PlaywrightDispatcher(dispatcherConnection.rootDispatcher(), createPlaywright());

src/server/android/android.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ export interface Backend {
4343
export interface DeviceBackend {
4444
serial: string;
4545
status: string;
46-
close(): Promise<void>;
46+
close(): void;
4747
init(): Promise<void>;
4848
runCommand(command: string): Promise<Buffer>;
4949
open(command: string): Promise<SocketBackend>;
5050
}
5151

5252
export interface SocketBackend extends EventEmitter {
5353
write(data: Buffer): Promise<void>;
54-
close(): Promise<void>;
54+
close(): void;
5555
}
5656

5757
export class Android extends SdkObject {
@@ -176,7 +176,7 @@ export class AndroidDevice extends SdkObject {
176176
await this.installApk(await readFileAsync(require.resolve(`../../../bin/${file}`)));
177177

178178
debug('pw:android')('Starting the new driver');
179-
this.shell('am instrument -w com.microsoft.playwright.androiddriver.test/androidx.test.runner.AndroidJUnitRunner');
179+
this.shell('am instrument -w com.microsoft.playwright.androiddriver.test/androidx.test.runner.AndroidJUnitRunner').catch(e => debug('pw:android')(e));
180180
const socket = await this._waitForLocalAbstract('playwright_android_driver_socket');
181181
const transport = new Transport(socket, socket, socket, 'be');
182182
transport.onmessage = message => {
@@ -301,7 +301,7 @@ export class AndroidDevice extends SdkObject {
301301
await installSocket.write(content);
302302
const success = await new Promise(f => installSocket.on('data', f));
303303
debug('pw:android')('Written driver bytes: ' + success);
304-
await installSocket.close();
304+
installSocket.close();
305305
}
306306

307307
async push(content: Buffer, path: string, mode = 0o644): Promise<void> {
@@ -325,7 +325,7 @@ export class AndroidDevice extends SdkObject {
325325
const code = result.slice(0, 4).toString();
326326
if (code !== 'OKAY')
327327
throw new Error('Could not push: ' + code);
328-
await socket.close();
328+
socket.close();
329329
}
330330

331331
private async _refreshWebViews() {
@@ -420,7 +420,7 @@ Sec-WebSocket-Version: 13\r
420420
}
421421

422422
async close() {
423-
await this._socket!.close();
423+
this._socket!.close();
424424
}
425425
}
426426

src/server/android/backendAdb.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async function runCommand(command: string, serial?: string): Promise<Buffer> {
7575
} else {
7676
commandOutput = await socket.readAll();
7777
}
78-
await socket.close();
78+
socket.close();
7979
return commandOutput;
8080
}
8181

@@ -138,7 +138,7 @@ class BufferedSocketWrapper extends EventEmitter implements SocketBackend {
138138
await new Promise(f => this._socket.write(data, f));
139139
}
140140

141-
async close() {
141+
close() {
142142
if (this._isClosed)
143143
return;
144144
debug('pw:adb')('Close ' + this._command);

src/server/chromium/crDevTools.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class CRDevTools {
3333
this._savePromise = Promise.resolve();
3434
}
3535

36-
async install(session: CRSession) {
36+
install(session: CRSession) {
3737
session.on('Runtime.bindingCalled', async event => {
3838
if (event.name !== kBindingName)
3939
return;
@@ -66,7 +66,7 @@ export class CRDevTools {
6666
contextId: event.executionContextId
6767
}).catch(e => null);
6868
});
69-
await Promise.all([
69+
Promise.all([
7070
session.send('Runtime.enable'),
7171
session.send('Runtime.addBinding', { name: kBindingName }),
7272
session.send('Page.enable'),

src/server/chromium/crPage.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,10 @@ class FrameSession {
646646
session.once('Runtime.executionContextCreated', async event => {
647647
worker._createExecutionContext(new CRExecutionContext(session, event.context));
648648
});
649-
Promise.all([
650-
session._sendMayFail('Runtime.enable'),
651-
session._sendMayFail('Network.enable'),
652-
session._sendMayFail('Runtime.runIfWaitingForDebugger'),
653-
]); // This might fail if the target is closed before we initialize.
649+
// This might fail if the target is closed before we initialize.
650+
session._sendMayFail('Runtime.enable');
651+
session._sendMayFail('Network.enable');
652+
session._sendMayFail('Runtime.runIfWaitingForDebugger');
654653
session.on('Runtime.consoleAPICalled', event => {
655654
const args = event.args.map(o => worker._existingExecutionContext!.createHandle(o));
656655
this._page._addConsoleMessage(event.type, args, toConsoleMessageLocation(event.stackTrace));

src/server/download.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class Download {
8383
throw new Error('Download not found on disk. Check download.failure() for details.');
8484

8585
if (this._finished) {
86-
saveCallback(path.join(this._downloadsPath, this._uuid));
86+
saveCallback(path.join(this._downloadsPath, this._uuid)).catch(e => {});
8787
return;
8888
}
8989
this._saveCallbacks.push(saveCallback);
@@ -117,7 +117,7 @@ export class Download {
117117
const fileName = path.join(this._downloadsPath, this._uuid);
118118
await util.promisify(fs.unlink)(fileName).catch(e => {});
119119
}
120-
this._reportFinished('Download deleted upon browser context closure.');
120+
await this._reportFinished('Download deleted upon browser context closure.');
121121
}
122122

123123
async _reportFinished(error?: string) {
@@ -128,7 +128,7 @@ export class Download {
128128

129129
if (error) {
130130
for (const callback of this._saveCallbacks)
131-
callback('', error);
131+
await callback('', error);
132132
} else {
133133
const fullPath = path.join(this._downloadsPath, this._uuid);
134134
for (const callback of this._saveCallbacks)

0 commit comments

Comments
 (0)