Skip to content

Commit

Permalink
fix(base-driver): Use WeakRef to reference the driver instance in the…
Browse files Browse the repository at this point in the history
… log prefix generator (#16636)
  • Loading branch information
mykola-mokhnach committed Mar 23, 2022
1 parent 21d337f commit bbfc7ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/appium/lib/appium.js
Expand Up @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions packages/base-driver/lib/basedriver/driver.js
Expand Up @@ -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;
Expand Down

0 comments on commit bbfc7ef

Please sign in to comment.