You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
publicstaticvoidmain(String[] args) throwsException {
varplaywright = Playwright.create();
varbrowser = playwright.firefox().launch(newBrowserType.LaunchOptions().withHeadless(true));
for (inti = 0; i < 100; i++) {
newThread(() -> {
// create the component, seems it will be registered in the connection objectvarcontext = browser.newContext();
System.out.println("create ok");
// use the component, it will get from the connection object but not foundcontext.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!)
The text was updated successfully, but these errors were encountered:
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
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.
I found that when doing operation with the same playwright object under multi-thread may throw
PlaywrightException: Object doesn't exist: BrowserContext@3453a9326319bd981cb8ebe57b0c34d2
orPlaywrightException: 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 byConnection.registerObject()
and connection use a
HashMap
to do so, butHashMap
is not thread safetywhen
HashMap
expand its size under multi-thread, some data will loseand
PlaywrightException: Cannot find command to respond
seems to be caused by the same reason, thatConnection.internalSendMessage()
put the message in thecallbacks
attribute which is aHashMap
test code like this:
(I'm not a native English speaker, and I'm a beginner of coding so if there something wrong, please tell me, Thanks!)
The text was updated successfully, but these errors were encountered: