fix(base-driver): Use WeakRef to reference the driver instance in the log prefix generator
MODIFY
packages/appium/lib/appium.js
packages/appium/lib/appium.js
@@ -66,7 +66,7 @@ class AppiumDriver extends BaseDriver {
*/
get log () {
if (!this._log) {
- const instanceName = `${this.constructor.name}@${node.getObjectId(this).substring(0, 8)}`;
+ const instanceName = `${this.constructor.name}@${node.getObjectId(this).substring(0, 4)}`;
this._log = logger.getLogger(instanceName);
}
return this._log;

MODIFY
packages/base-driver/lib/basedriver/driver.js
packages/base-driver/lib/basedriver/driver.js
@@ -108,10 +108,21 @@ class BaseDriver extends Protocol {
get log () {
if (!this._log) {
- const instanceName = `${this.constructor.name}@${node.getObjectId(this).substring(0, 8)}`;
- this._log = logger.getLogger(() =>
- this.sessionId ? `${instanceName} (${this.sessionId.substring(0, 8)})` : instanceName
- );
+ const instanceName = `${this.constructor.name}@${node.getObjectId(this).substring(0, 4)}`;
+ // We don't want the self reference to be captured inside the below closure
+ // to avoid possible memory leaks,
+ // but NodeJS started supporting WeakRef only since v. 14.6
+ const self = global.WeakRef ? new global.WeakRef(this) : this;
+ this._log = logger.getLogger(() => {
+ let sessionId = self?.sessionId;
+ if (!sessionId && _.isFunction(self.deref)) {
+ const ref = self.deref();
+ if (ref?.sessionId) {
+ sessionId = ref.sessionId;
+ }
+ }
+ return sessionId ? `${instanceName} (${sessionId.substring(0, 8)})` : instanceName;
+ });
}
return this._log;

fix(base-driver): Use WeakRef to reference the driver instance in the log prefix generator (#16636)