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

[Question] does playwright object design to be thread safety? #212

Closed
Farley-Chen opened this issue Jan 17, 2021 · 1 comment
Closed

[Question] does playwright object design to be thread safety? #212

Farley-Chen opened this issue Jan 17, 2021 · 1 comment

Comments

@Farley-Chen
Copy link

Farley-Chen commented Jan 17, 2021

I found that when doing operation with the same playwright object under multi-thread may throw PlaywrightException: Object doesn't exist: BrowserContext@3453a9326319bd981cb8ebe57b0c34d2 or PlaywrightException: Cannot find command to respond,

PlaywrightException: Object doesn't exist's cause seems like that every component create by the same playwright object should register to the same connection object by Connection.registerObject()

and connection use a HashMap to do so, but HashMap is not thread safety

when HashMap expand its size under multi-thread, some data will lose

and PlaywrightException: Cannot find command to respond seems to be caused by the same reason, that Connection.internalSendMessage() put the message in the callbacks attribute which is a HashMap

test code like this:

 public static void main(String[] args) throws Exception {
        var playwright = Playwright.create();
        var browser = playwright.firefox().launch(new BrowserType.LaunchOptions().withHeadless(true));
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                // create the component, seems it will be registered in the connection object
                var context = browser.newContext();
                System.out.println("create ok");
                // use the component, it will get from the connection object but not found
                context.cookies("http://www.bing.com");
            }).start();
        }
        Thread.sleep(30000);
        playwright.close();
    }

(I'm not a native English speaker, and I'm a beginner of coding so if there something wrong, please tell me, Thanks!)

@Farley-Chen Farley-Chen changed the title [Question] does Connection.registerObject() design to be thread safety? [Question] does playwright object design to be thread safety? Jan 17, 2021
@yury-s
Copy link
Member

yury-s commented Jan 19, 2021

No, Playwright is not thread safe, i.e. all its methods as well as methods on all objects created by it (such as BrowserContext, Browser, Page etc.) are expected to be called from the same thread where Playwright object was created or proper synchronization should be implemented to ensure only one thread calls playwright methods at any given time. Having said that it's okay to create new playwright instance on a new thread. I'll update README.md to reflect this.

@yury-s yury-s closed this as completed Jan 19, 2021
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

No branches or pull requests

2 participants