Skip to content

Implement SiteDataManager::cookies_for_url_async#43794

Merged
jdm merged 4 commits into
servo:mainfrom
longvatrong111:get-cookie-async
Apr 17, 2026
Merged

Implement SiteDataManager::cookies_for_url_async#43794
jdm merged 4 commits into
servo:mainfrom
longvatrong111:get-cookie-async

Conversation

@longvatrong111

Copy link
Copy Markdown
Contributor

Support applications to get cookies asynchronously from embedder.

Testing: servo/components/servo/tests/site_data_manager.rs::test_get_cookie_async

@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 31, 2026
@codecov-commenter

codecov-commenter commented Mar 31, 2026

Copy link
Copy Markdown

❌ 4 Tests Failed:

Tests completed Failed Passed Skipped
894 4 890 0
View the top 3 failed test(s) by shortest run time
servo::site_data_manager::test_clear_site_data_cookies
Stack Traces | 0.188s run time
thread 'test_clear_site_data_cookies' (59642) panicked at components/servo/servo.rs:244:14:
RefCell already borrowed
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::cell::panic_already_borrowed::do_panic::runtime
   3: core::cell::panic_already_borrowed
   4: servo::servo::ServoInner::spin_event_loop
   5: site_data_manager::run_test_site_data_steps
   6: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
servo::site_data_manager::test_clear_site_data_session
Stack Traces | 0.196s run time
thread 'test_clear_site_data_session' (59712) panicked at components/servo/servo.rs:244:14:
RefCell already borrowed
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::cell::panic_already_borrowed::do_panic::runtime
   3: core::cell::panic_already_borrowed
   4: servo::servo::ServoInner::spin_event_loop
   5: site_data_manager::run_test_site_data_steps
   6: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
servo::site_data_manager::test_site_data
Stack Traces | 0.206s run time
thread 'test_site_data' (60562) panicked at components/servo/servo.rs:244:14:
RefCell already borrowed
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::cell::panic_already_borrowed::do_panic::runtime
   3: core::cell::panic_already_borrowed
   4: servo::servo::ServoInner::spin_event_loop
   5: site_data_manager::run_test_site_data_steps
   6: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
servo::site_data_manager::test_clear_site_data_local
Stack Traces | 0.233s run time
thread 'test_clear_site_data_local' (59644) panicked at components/servo/servo.rs:244:14:
RefCell already borrowed
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::cell::panic_already_borrowed::do_panic::runtime
   3: core::cell::panic_already_borrowed
   4: servo::servo::ServoInner::spin_event_loop
   5: site_data_manager::run_test_site_data_steps
   6: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@longvatrong111 longvatrong111 marked this pull request as draft March 31, 2026 10:07
@longvatrong111 longvatrong111 force-pushed the get-cookie-async branch 2 times, most recently from 6d1926e to 27da176 Compare March 31, 2026 11:24
Comment thread components/servo/servo.rs Outdated
@longvatrong111 longvatrong111 marked this pull request as ready for review April 1, 2026 07:56
@longvatrong111

longvatrong111 commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

@mrobinson this PR is ready, can you help review? Thank you.

@jdm

jdm commented Apr 11, 2026

Copy link
Copy Markdown
Member

This implementation works, and I can see the appeal of reusing the existing GetCookiesForUrl message. However, polling while spinning the event loop has a subtle performance issue—if there's nothing else happening that triggers an event loop spin, the response will sit unprocessed. In the case of servoshell, this would happen if the page isn't animating and the user doesn't trigger any inputs:

if !self.pump_servo_event_loop(event_loop.into()) {
event_loop.exit();
}
// Block until the window gets an event
event_loop.set_control_flow(ControlFlow::Wait);

Is this a big deal? Hard to say; I could imagine some embedding scenarios where pauses without user inputs could happen. However, there's another design possible which would avoid any polling:

  1. add a new CookieOperationId type
  2. add a new CoreResourceMsg GetCookiesForUrlAsync message that takes a CookieOperationId, CookieSource, and URL
  3. add a response message to NetToEmbedderMsg in components/net/embedder.rs that takes a CookieOperationId and Vec<Cookie<'static>>
  4. add a GenericEmbedderProxy<NetToEmbedderMsg> member to ResourceChannelManager
  5. when the resource thread processes the GetCookiesForUrlAsync, send the response on the new embedder proxy
  6. when the new NetToEmbedderMsg message is received, the site data manager invokes the appropriate pending cookie request callback

@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Apr 11, 2026
@longvatrong111

Copy link
Copy Markdown
Contributor Author
  1. add a GenericEmbedderProxy<NetToEmbedderMsg> member to ResourceChannelManager
  2. when the resource thread processes the GetCookiesForUrlAsync, send the response on the new embedder proxy
  3. when the new NetToEmbedderMsg message is received, the site data manager invokes the appropriate pending cookie request callback

I think it's worth to implement a more comprehensive solution. I have a draft at local. There are some points need to discuss:

  • When resource thread process_msg, we already has a http_state which contains a embedder_proxy, so step 4 and 5 can be done without a new proxy?
    fn process_msg(
    &mut self,
    msg: CoreResourceMsg,
    http_state: &Arc<HttpState>,
    protocols: Arc<ProtocolRegistry>,
  • However, I don't know how to invoke pending callback without the spin loop. As I know, Servo also handles NetToEmbedderMessage in the spin loop:
    Message::FromNet(message) => self.handle_net_embedder_message(message),

@jdm

jdm commented Apr 13, 2026

Copy link
Copy Markdown
Member
  • However, I don't know how to invoke pending callback without the spin loop. As I know, Servo also handles NetToEmbedderMessage in the spin loop:

The difference is that GenericEmbedderProxy contains an event loop waker, so any message sent on the channel also notifies the embedder to spin the event loop.

@jdm

jdm commented Apr 13, 2026

Copy link
Copy Markdown
Member

When resource thread process_msg, we already has a http_state which contains a embedder_proxy, so step 4 and 5 can be done without a new proxy?

If you can access an existing embedder proxy then you can use that.

@mrobinson

Copy link
Copy Markdown
Member

General question: How important is the asynchronous version? The other APIs in SiteDataManager work synchronously. I would rather add support for asynchronous operation to all APIs, rather than just a special asynchronous version of a single API point.

@longvatrong111

Copy link
Copy Markdown
Contributor Author

General question: How important is the asynchronous version? The other APIs in SiteDataManager work synchronously. I would rather add support for asynchronous operation to all APIs, rather than just a special asynchronous version of a single API point.

@mrobinson
The purpose is to support HOS WebCookieManager.
There is a pair of sync and async for each API: get cookie, set cookie, save cookie, delete all cookies and delete session cookie.
The get cookie is the highest priority.
Do you want me to add more in this PR?

@servo-highfive servo-highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Apr 14, 2026
@servo-highfive servo-highfive added the S-needs-rebase There are merge conflict errors. label Apr 14, 2026
Signed-off-by: batu_hoang <longvatrong111@gmail.com>
Signed-off-by: batu_hoang <longvatrong111@gmail.com>
Remove polling which depends on Servo spin loop.
Instead, use an id when making a request to resource thread.
Net thread will notify embedder with the id when request is done.

Signed-off-by: batu_hoang <longvatrong111@gmail.com>
@servo-highfive servo-highfive removed the S-needs-rebase There are merge conflict errors. label Apr 14, 2026
@longvatrong111 longvatrong111 requested a review from jdm April 15, 2026 09:25
Signed-off-by: batu_hoang <longvatrong111@gmail.com>
@longvatrong111

Copy link
Copy Markdown
Contributor Author

@mrobinson I've updated the solution to handle asynchronous callback and response data for all APIs, and implement set_cookie_for_url_async.
Other async APIs will be addressed in #44166.

@jdm jdm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good! Thanks for making the changes.

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Apr 17, 2026
@jdm jdm added this pull request to the merge queue Apr 17, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Apr 17, 2026
Merged via the queue into servo:main with commit 2d445b6 Apr 17, 2026
30 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Apr 17, 2026
@longvatrong111 longvatrong111 deleted the get-cookie-async branch May 13, 2026 17:59
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.

6 participants