Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] IJavascriptEngine implements IDisposable where available #11594

Merged
merged 1 commit into from
Feb 17, 2023
Merged

[dotnet] IJavascriptEngine implements IDisposable where available #11594

merged 1 commit into from
Feb 17, 2023

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Jan 27, 2023

JavascriptExecutor takes an IWebDriver and, on a lazy basis, opens a DevToolsSession which it currently leaves open. This PR disposes on that DevToolsSession if it was instantiated.

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

Closes the DevToolsSession in JavascriptEngine if it's been opened.

Motivation and Context

cq

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@CLAassistant
Copy link

CLAassistant commented Jan 27, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@diemol diemol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test or something we can see the difference in behavior? The explanation makes sense, but I really don't understand Dispose at all and don't see how the description results in that code?

@RenderMichael
Copy link
Contributor Author

RenderMichael commented Feb 16, 2023

Is there a test or something we can see the difference in behavior?

This doesn't change behavior, it's just that there's an open session hanging around, beyond the lifespan of the IJavascriptEngine.

I really don't understand Dispose at all and don't see how the description results in that code?

Sure, so the JavascriptEngine class has a private Lazy<DevToolsSession> object named session which is, as the class name suggests, lazily evaluated whenever it's accessed. So there's a DevToolsSession owned by this class.

However, DevToolsSession is itself an IDisposable type, which needs to have Dispose() called on it before it's out of reach. Maybe it has an open network connection, or something that doesn't just go away when it's no longer referenced, not sure.

However, as it stands, DevToolsSession.Dispose() is never called on the session object. Without looking into the implementation details of DevToolsSession, it's hard to say what this means, only that since it implements IDisposable, developers expect Dispose() to be called.

This PR passes that along to the JavascriptEngine class which owns session, such that disposing the JavascriptEngine will dispose the underlying session, if it had been lazily evaluated.

@RenderMichael
Copy link
Contributor Author

don't see how the description results in that code

This is the standard dispose pattern, where Dispose() is a sealed method, while Dispose(bool) can be overridden by a derived class. So if another class extends JavascriptEngine, they can do

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        // whatever additional disposal you need
    }
    base.Dispose(disposing);
}

but users of the derived class still only need to call Dispose() to do it all

Copy link
Member

@diemol diemol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation, makes sense. I will wait for the CI and then merge.

Looking forward to future contributions!

JavascriptExecutor takes an IWebDriver and, on a lazy basis, opens a
DevToolsSession which it currently leaves open. This PR disposes on
that DevToolsSession if it was instantiated.
@diemol diemol merged commit c1ac4c7 into SeleniumHQ:trunk Feb 17, 2023
@RenderMichael RenderMichael deleted the dotnet-js-engine-dispose branch February 17, 2023 16:17
RenderMichael added a commit to RenderMichael/seleniumhq.github.io that referenced this pull request Apr 5, 2023
After SeleniumHQ/selenium#11594 was merged in v4.8.1, IJavascriptEngine now implements IDisposable and should be disposed of with the `using` statement, as is standard practice.
RenderMichael added a commit to RenderMichael/seleniumhq.github.io that referenced this pull request Apr 5, 2023
After SeleniumHQ/selenium#11594 was merged in v4.8.1,
IJavascriptEngine now implements IDisposable and should be disposed of with the `using` statement, as is standard practice.
harsha509 pushed a commit to SeleniumHQ/seleniumhq.github.io that referenced this pull request Apr 11, 2023
After SeleniumHQ/selenium#11594 was merged in v4.8.1,
IJavascriptEngine now implements IDisposable and should be disposed of with the `using` statement, as is standard practice.
selenium-ci added a commit to SeleniumHQ/seleniumhq.github.io that referenced this pull request Apr 11, 2023
After SeleniumHQ/selenium#11594 was merged in v4.8.1,
IJavascriptEngine now implements IDisposable and should be disposed of with the `using` statement, as is standard practice. 0eeeeca
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants