@@ -30,7 +30,6 @@ import { Waiter } from './waiter';
3030import type { URLMatch , Headers , WaitForEventOptions , BrowserContextOptions , StorageState , LaunchOptions } from './types' ;
3131import { headersObjectToArray , isRegExp , isString } from '../utils' ;
3232import { mkdirIfNeeded } from '../utils/fileUtils' ;
33- import { isSafeCloseError } from '../common/errors' ;
3433import type * as api from '../../types/types' ;
3534import type * as structs from '../../types/structs' ;
3635import { CDPSession } from './cdpSession' ;
@@ -59,6 +58,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
5958 readonly _serviceWorkers = new Set < Worker > ( ) ;
6059 readonly _isChromium : boolean ;
6160 private _harRecorders = new Map < string , { path : string , content : 'embed' | 'attach' | 'omit' | undefined } > ( ) ;
61+ private _closeWasCalled = false ;
6262
6363 static from ( context : channels . BrowserContextChannel ) : BrowserContext {
6464 return ( context as any ) . _object ;
@@ -344,31 +344,28 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
344344 }
345345
346346 async close ( ) : Promise < void > {
347- try {
348- await this . _wrapApiCall ( async ( ) => {
349- await this . _browserType ?. _onWillCloseContext ?. ( this ) ;
350- for ( const [ harId , harParams ] of this . _harRecorders ) {
351- const har = await this . _channel . harExport ( { harId } ) ;
352- const artifact = Artifact . from ( har . artifact ) ;
353- // Server side will compress artifact if content is attach or if file is .zip.
354- const isCompressed = harParams . content === 'attach' || harParams . path . endsWith ( '.zip' ) ;
355- const needCompressed = harParams . path . endsWith ( '. zip' ) ;
356- if ( isCompressed && ! needCompressed ) {
357- await artifact . saveAs ( harParams . path + '.tmp ') ;
358- await this . _connection . localUtils ( ) . _channel . harUnzip ( { zipFile : harParams . path + '.tmp' , harFile : harParams . path } ) ;
359- } else {
360- await artifact . saveAs ( harParams . path ) ;
361- }
362- await artifact . delete ( ) ;
347+ if ( this . _closeWasCalled )
348+ return ;
349+ this . _closeWasCalled = true ;
350+ await this . _wrapApiCall ( async ( ) => {
351+ await this . _browserType ?. _onWillCloseContext ?. ( this ) ;
352+ for ( const [ harId , harParams ] of this . _harRecorders ) {
353+ const har = await this . _channel . harExport ( { harId } ) ;
354+ const artifact = Artifact . from ( har . artifact ) ;
355+ // Server side will compress artifact if content is attach or if file is . zip.
356+ const isCompressed = harParams . content === 'attach' || harParams . path . endsWith ( '.zip' ) ;
357+ const needCompressed = harParams . path . endsWith ( '.zip ') ;
358+ if ( isCompressed && ! needCompressed ) {
359+ await artifact . saveAs ( harParams . path + '.tmp' ) ;
360+ await this . _connection . localUtils ( ) . _channel . harUnzip ( { zipFile : harParams . path + '.tmp' , harFile : harParams . path } ) ;
361+ } else {
362+ await artifact . saveAs ( harParams . path ) ;
363363 }
364- } , true ) ;
365- await this . _channel . close ( ) ;
366- await this . _closedPromise ;
367- } catch ( e ) {
368- if ( isSafeCloseError ( e ) )
369- return ;
370- throw e ;
371- }
364+ await artifact . delete ( ) ;
365+ }
366+ } , true ) ;
367+ await this . _channel . close ( ) ;
368+ await this . _closedPromise ;
372369 }
373370
374371 async _enableRecorder ( params : {
0 commit comments