fix: make sure the listener gets removed after the command executor is completed
MODIFY
lib/basedriver/driver.js
lib/basedriver/driver.js
@@ -323,12 +323,22 @@ class BaseDriver extends Protocol {
throw new errors.NotYetImplementedError();
}
+ let unexpectedShutdownListener;
const commandExecutor = async () => imgElId
? await ImageElement.execute(this, cmd, imgElId, ...args)
: await B.race([
this[cmd](...args),
- new B((resolve, reject) => this.eventEmitter.on(ON_UNEXPECTED_SHUTDOWN_EVENT, reject))
- ]);
+ new B((resolve, reject) => {
+ unexpectedShutdownListener = reject;
+ this.eventEmitter.on(ON_UNEXPECTED_SHUTDOWN_EVENT, unexpectedShutdownListener);
+ })
+ ]).finally(() => {
+ if (unexpectedShutdownListener) {
+ // This is needed to prevent memory leaks
+ this.eventEmitter.removeListener(ON_UNEXPECTED_SHUTDOWN_EVENT, unexpectedShutdownListener);
+ unexpectedShutdownListener = null;
+ }
+ });
const res = this.isCommandsQueueEnabled && cmd !== 'executeDriverScript'
? await this.commandsQueueGuard.acquire(BaseDriver.name, commandExecutor)
: await commandExecutor();

fix: make sure the listener gets removed after the command executor is completed (#371)