servo-fetch 0.15.0

Fetch, render, and extract web content as Markdown, JSON, or screenshots with an embedded Servo browser engine. No Chromium required.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! Worker process ownership, lifecycle supervision, and resource cleanup.

use std::io::{BufReader, BufWriter};
#[cfg(unix)]
use std::os::fd::{AsRawFd as _, OwnedFd};
use std::path::PathBuf;
use std::process::{Child, ChildStdin, Command, ExitStatus, Stdio};
use std::sync::Arc;
use std::time::{Duration, Instant};

use crossbeam_channel::{Receiver, Sender};
use tokio::sync::OwnedSemaphorePermit;

use super::{SessionCancellation, WorkerCommand, cancelled_error, is_terminal_session_error};
use crate::CrawlResult;
use crate::cookies::CookieSpec;
use crate::error::{Error, Result};
#[cfg(windows)]
use crate::sys::windows::{resume_suspended_process, suspend_new_process};
use crate::worker::protocol::{
    InitializeSession, PACKAGE_VERSION, RequestFrame, ResponseFrame, WorkerProtocolInfo, WorkerRequest, WorkerResponse,
    decode_frame, read_bounded_frame, validate_response, write_bounded_frame,
};
use crate::worker::wire::{
    CrawlWire, FetchWire, MAX_SCREENSHOT_BYTES, PageWire, crawl_wire_absolute_watchdog, crawl_wire_watchdog,
    fetch_wire_watchdog,
};
use crate::worker::{
    MAX_WORKER_BLOB_CHUNK_BYTES, MAX_WORKER_FRAME_BYTES, MAX_WORKER_PROTOCOL_INFO_BYTES,
    MAX_WORKER_REQUEST_FRAME_BYTES, WORKER_PROTOCOL_MAGIC, worker_error,
};

mod kill;
mod process_tree;

use kill::KillAuthority;
#[cfg(unix)]
pub(in crate::session) use process_tree::create_parent_lifeline;
#[cfg(unix)]
use process_tree::inherit_parent_lifeline;
use process_tree::{ProcessTree, observe_child_exit_without_reaping};

const BOOTSTRAP_TIMEOUT: Duration = Duration::from_secs(10);
const REAP_GRACE: Duration = Duration::from_secs(2);
#[derive(Debug, Clone, Copy)]
enum Lifecycle {
    Force,
}

type ProcessTreeAuthority = Arc<KillAuthority<ProcessTree>>;

/// Kills the worker process tree directly so a stuck supervisor cannot block cancellation.
#[derive(Debug, Clone)]
pub(super) struct ForcePort {
    lifecycle: Sender<Lifecycle>,
    authority: ProcessTreeAuthority,
}

impl ForcePort {
    pub(super) fn force(&self) {
        // Coalesce the supervisor notification while still issuing a prompt kill.
        let _ = self.lifecycle.try_send(Lifecycle::Force);
        self.authority.terminate_now();
    }
}

pub(super) type ResponseSender<T> = Sender<Result<T>>;
pub(super) type ResponseReceiver<T> = Receiver<Result<T>>;

pub(super) fn frame_wait_duration(idle_timeout: Duration, remaining: Duration) -> Option<Duration> {
    (!remaining.is_zero()).then(|| idle_timeout.min(remaining))
}

pub(super) fn response_channel<T>() -> (ResponseSender<T>, ResponseReceiver<T>) {
    crossbeam_channel::bounded(1)
}

pub(super) enum SupervisorCommand {
    Initialize {
        request: InitializeSession,
        reply: ResponseSender<()>,
    },
    Fetch {
        request: FetchWire,
        reply: ResponseSender<(PageWire, Option<Vec<u8>>)>,
    },
    Cookies {
        url: String,
        reply: ResponseSender<Vec<CookieSpec>>,
    },
    Crawl {
        request: CrawlWire,
        events: Sender<CrawlResult>,
        reply: ResponseSender<()>,
    },
    Shutdown {
        reply: ResponseSender<()>,
    },
}

pub(super) struct SupervisorHandle {
    commands: Sender<SupervisorCommand>,
    pub(super) force_port: ForcePort,
    terminal: Option<ResponseReceiver<()>>,
    pub(super) config_dir: PathBuf,
    armed: bool,
}

impl std::fmt::Debug for SupervisorHandle {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("SupervisorHandle")
            .field("config_dir", &self.config_dir)
            .field("armed", &self.armed)
            .finish_non_exhaustive()
    }
}

impl SupervisorHandle {
    pub(super) fn spawn(
        command: WorkerCommand,
        permit: OwnedSemaphorePermit,
        cancellation: Option<&SessionCancellation>,
    ) -> Result<Self> {
        let (commands, command_rx) = crossbeam_channel::bounded(1);
        let (lifecycle, lifecycle_rx) = crossbeam_channel::bounded(1);
        let (bootstrap_result, bootstrap_result_rx) = response_channel();
        let (terminal, terminal_rx) = response_channel();
        let force_port = ForcePort {
            lifecycle,
            authority: Arc::new(KillAuthority::new()),
        };
        if cancellation.is_some_and(|cancel| !cancel.attach(&force_port)) {
            return Err(cancelled_error());
        }
        let authority = Arc::clone(&force_port.authority);
        std::thread::Builder::new()
            .name("servo-fetch-supervisor".into())
            .spawn(move || {
                let result = supervisor_thread(command, permit, command_rx, lifecycle_rx, authority, bootstrap_result);
                if let Err(send_error) = terminal.send(result)
                    && let Err(error) = send_error.into_inner()
                {
                    tracing::error!(%error, "detached supervisor cleanup failed");
                }
            })
            .map_err(worker_error)?;
        let config_dir = receive_response(&bootstrap_result_rx, "worker protocol bootstrap")?;
        Ok(Self {
            commands,
            force_port,
            terminal: Some(terminal_rx),
            config_dir,
            armed: true,
        })
    }

    pub(super) fn send(&self, command: SupervisorCommand) -> Result<()> {
        self.commands
            .send(command)
            .map_err(|_| worker_error("worker supervisor stopped"))
    }

    pub(super) fn initialize_session(&mut self, request: InitializeSession) -> Result<()> {
        let (reply, receive) = response_channel();
        self.send(SupervisorCommand::Initialize { request, reply })?;
        receive_response(&receive, "worker session initialization")
    }

    pub(super) fn begin_close(&mut self) -> Result<ResponseReceiver<()>> {
        let (reply, receive) = response_channel();
        self.send(SupervisorCommand::Shutdown { reply })?;
        Ok(receive)
    }

    pub(super) fn close_blocking(&mut self) -> Result<()> {
        let receive = self.begin_close()?;
        receive_response(&receive, "session close")
    }

    pub(super) fn begin_force(&mut self) -> Result<ResponseReceiver<()>> {
        self.force();
        self.terminal
            .take()
            .ok_or_else(|| worker_error("supervisor terminal receiver unavailable"))
    }

    pub(super) fn force(&mut self) {
        if self.armed {
            self.force_port.force();
            self.armed = false;
        }
    }

    pub(super) fn disarm(&mut self) {
        self.armed = false;
    }
}

impl Drop for SupervisorHandle {
    fn drop(&mut self) {
        // Drop only enqueues force-close; the supervisor owns teardown.
        self.force();
    }
}

pub(super) async fn recv_optional_async<T: Send + 'static>(receive: Receiver<T>) -> Result<Option<T>> {
    tokio::task::spawn_blocking(move || receive.recv().ok())
        .await
        .map_err(|error| worker_error(format!("crawl event task failed: {error}")))
}

pub(super) async fn recv_async<T: Send + 'static>(receive: ResponseReceiver<T>, context: &'static str) -> Result<T> {
    tokio::task::spawn_blocking(move || receive_response(&receive, context))
        .await
        .map_err(|error| worker_error(format!("{context} task failed: {error}")))?
}

pub(super) fn receive_response<T>(receive: &ResponseReceiver<T>, context: &str) -> Result<T> {
    receive
        .recv()
        .map_err(|_| worker_error(format!("{context} response channel closed")))?
}

pub(super) struct SupervisorOwner {
    config_dir: Option<PathBuf>,
    permit: Option<OwnedSemaphorePermit>,
}

impl SupervisorOwner {
    pub(super) fn new(permit: OwnedSemaphorePermit) -> Result<Self> {
        let temp_dir = tempfile::Builder::new()
            .prefix("servo-fetch-session-")
            .tempdir()
            .map_err(worker_error)?;
        super::scavenge::write_owner_marker(temp_dir.path());
        Ok(Self {
            config_dir: Some(temp_dir.keep()),
            permit: Some(permit),
        })
    }

    pub(super) fn config_dir(&self) -> PathBuf {
        self.config_dir.as_ref().expect("supervisor tempdir is present").clone()
    }

    fn release_with(&mut self, mut remove: impl FnMut(&std::path::Path) -> std::io::Result<()>) -> Result<()> {
        if let Some(path) = self.config_dir.as_deref() {
            let mut last_error = None;
            for _ in 0..3 {
                match remove(path) {
                    Ok(()) => {
                        last_error = None;
                        break;
                    }
                    Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                        last_error = None;
                        break;
                    }
                    Err(error) => {
                        last_error = Some(error);
                        std::thread::sleep(Duration::from_millis(10));
                    }
                }
            }
            if let Some(error) = last_error {
                return Err(worker_error(format!(
                    "failed to delete browser session storage at {}: {error}",
                    path.display()
                )));
            }
            self.config_dir.take();
        }
        self.permit.take();
        Ok(())
    }

    pub(super) fn release(&mut self) -> Result<()> {
        self.release_with(|path| std::fs::remove_dir_all(path))
    }
}

impl Drop for SupervisorOwner {
    fn drop(&mut self) {
        if let Err(error) = self.release() {
            if let Some(permit) = self.permit.take() {
                std::mem::forget(permit);
            }
            tracing::error!(%error, "supervisor owner cleanup failed; retaining broker capacity");
        }
    }
}

fn cleanup_spawned_child(
    child: &mut Child,
    process_tree: &ProcessTree,
    authority: &ProcessTreeAuthority,
) -> Result<()> {
    drop(authority.revoke());
    process_tree.terminate(child).map_err(worker_error)?;
    child.wait().map_err(worker_error)?;
    #[cfg(windows)]
    process_tree.wait_quiescent().map_err(worker_error)?;
    Ok(())
}

struct WorkerProcess {
    child: Option<Child>,
    process_tree: Arc<ProcessTree>,
    authority: ProcessTreeAuthority,
    stdin: Option<BufWriter<ChildStdin>>,
    frames: Option<Receiver<std::io::Result<Vec<u8>>>>,
    reader: Option<std::thread::JoinHandle<()>>,
    #[cfg(unix)]
    parent_lifeline: Option<OwnedFd>,
    owner: SupervisorOwner,
    lifecycle: Receiver<Lifecycle>,
    termination_started: bool,
    exit_status: Option<ExitStatus>,
    quiescent: bool,
    next_id: u64,
}

impl WorkerProcess {
    fn spawn(
        command: &WorkerCommand,
        permit: OwnedSemaphorePermit,
        lifecycle: Receiver<Lifecycle>,
        authority: ProcessTreeAuthority,
    ) -> Result<Self> {
        command.validate()?;
        if lifecycle.try_recv().is_ok() {
            return Err(cancelled_error());
        }
        let owner = SupervisorOwner::new(permit)?;
        #[cfg(unix)]
        let (lifeline_read, lifeline_write) = create_parent_lifeline()?;
        let mut process = Command::new(&command.program);
        process
            .args(&command.args)
            .current_dir(owner.config_dir())
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::inherit());
        #[cfg(unix)]
        inherit_parent_lifeline(&mut process, lifeline_read.as_raw_fd());
        #[cfg(windows)]
        suspend_new_process(&mut process);
        let mut child = process.spawn().map_err(worker_error)?;
        #[cfg(unix)]
        drop(lifeline_read);

        let process_tree = match ProcessTree::attach(&child).map(Arc::new) {
            Ok(process_tree) => process_tree,
            Err(error) => {
                let cleanup = child.kill().and_then(|()| child.wait().map(drop)).map_err(worker_error);
                return with_cleanup(Err(error), cleanup);
            }
        };
        authority.grant(Arc::clone(&process_tree));
        let fail = |child: &mut Child, error: Error| -> Result<Self> {
            with_cleanup(Err(error), cleanup_spawned_child(child, &process_tree, &authority))
        };

        if lifecycle.try_recv().is_ok() {
            return fail(&mut child, cancelled_error());
        }
        #[cfg(windows)]
        if let Err(error) = resume_suspended_process(&child).map_err(worker_error) {
            return fail(&mut child, error);
        }

        let Some(stdin) = child.stdin.take() else {
            return fail(&mut child, worker_error("worker stdin unavailable"));
        };
        let Some(stdout) = child.stdout.take() else {
            return fail(&mut child, worker_error("worker stdout unavailable"));
        };
        let (frames_tx, frames) = crossbeam_channel::bounded(1);
        let reader = std::thread::Builder::new()
            .name("servo-fetch-worker-reader".into())
            .spawn(move || {
                let mut reader = BufReader::new(stdout);
                let mut first_frame = true;
                loop {
                    let max = if first_frame {
                        MAX_WORKER_PROTOCOL_INFO_BYTES
                    } else {
                        MAX_WORKER_FRAME_BYTES
                    };
                    let frame = read_bounded_frame(&mut reader, max);
                    if frame.is_ok() {
                        first_frame = false;
                    }
                    let done = frame
                        .as_ref()
                        .is_err_and(|error| error.kind() == std::io::ErrorKind::UnexpectedEof);
                    if frames_tx.send(frame).is_err() || done {
                        break;
                    }
                }
            });
        let reader = match reader {
            Ok(reader) => reader,
            Err(error) => return fail(&mut child, worker_error(error)),
        };
        Ok(Self {
            child: Some(child),
            process_tree,
            authority,
            stdin: Some(BufWriter::new(stdin)),
            frames: Some(frames),
            reader: Some(reader),
            #[cfg(unix)]
            parent_lifeline: Some(lifeline_write),
            owner,
            lifecycle,
            termination_started: false,
            exit_status: None,
            quiescent: false,
            next_id: 1,
        })
    }

    fn config_dir(&self) -> PathBuf {
        self.owner.config_dir()
    }

    fn receive_protocol_info(&mut self) -> Result<()> {
        let deadline = Instant::now() + BOOTSTRAP_TIMEOUT;
        let bytes = self.recv_frame(BOOTSTRAP_TIMEOUT, deadline, BOOTSTRAP_TIMEOUT, "protocol info")?;
        let info: WorkerProtocolInfo = decode_frame(&bytes)?;
        if info.magic != WORKER_PROTOCOL_MAGIC {
            return Err(worker_error(format!(
                "worker protocol mismatch: expected magic {WORKER_PROTOCOL_MAGIC:?}; got {:?}",
                info.magic
            )));
        }
        // Postcard encoding is not forward-compatible, so a worker built from a
        // different package version must be rejected before any request frame.
        if info.package_version != PACKAGE_VERSION {
            return Err(worker_error(format!(
                "worker package version mismatch: parent {PACKAGE_VERSION}, worker {}",
                info.package_version
            )));
        }
        Ok(())
    }

    fn initialize_session(&mut self, initialization: InitializeSession) -> Result<()> {
        let id = self.write_request(WorkerRequest::Initialize(initialization))?;
        match self.recv_response(id, BOOTSTRAP_TIMEOUT, "session initialization")? {
            WorkerResponse::SessionInitialized => Ok(()),
            WorkerResponse::Error(error) => Err(error.into_error()),
            _ => Err(worker_error("unexpected session initialization response")),
        }
    }

    fn cookies(&mut self, url: String) -> Result<Vec<CookieSpec>> {
        let id = self.write_request(WorkerRequest::Cookies { url })?;
        match self.recv_response(id, BOOTSTRAP_TIMEOUT, "cookies")? {
            WorkerResponse::Cookies(cookies) => crate::cookies::from_wire(cookies).map_err(worker_error),
            WorkerResponse::Error(error) => Err(error.into_error()),
            _ => Err(worker_error("unexpected cookies response")),
        }
    }

    fn fetch(&mut self, request: FetchWire) -> Result<(PageWire, Option<Vec<u8>>)> {
        let timeout = fetch_wire_watchdog(&request);
        let deadline = Instant::now() + timeout;
        let id = self.write_request(WorkerRequest::Fetch(request))?;
        let mut page = None;
        let mut screenshot = None;
        loop {
            let response = self.recv_response_until(id, timeout, deadline, timeout, "fetch")?;
            match response {
                WorkerResponse::FetchResult(result) if page.is_none() => {
                    let declared_size = result.screenshot_png_bytes();
                    if declared_size.is_some_and(|size| size > MAX_SCREENSHOT_BYTES) {
                        return Err(worker_error("declared screenshot payload exceeds maximum size"));
                    }
                    screenshot = declared_size.map(Vec::with_capacity);
                    page = Some(result);
                }
                WorkerResponse::ScreenshotChunk(chunk) => {
                    if chunk.is_empty() || chunk.len() > MAX_WORKER_BLOB_CHUNK_BYTES {
                        return Err(worker_error("invalid screenshot chunk size"));
                    }
                    let expected = page
                        .as_ref()
                        .and_then(PageWire::screenshot_png_bytes)
                        .ok_or_else(|| worker_error("unexpected screenshot chunk"))?;
                    let target = screenshot
                        .as_mut()
                        .ok_or_else(|| worker_error("screenshot payload was not declared"))?;
                    if chunk.len() > expected.saturating_sub(target.len()) {
                        return Err(worker_error("screenshot payload exceeds declared size"));
                    }
                    target.extend_from_slice(&chunk);
                }
                WorkerResponse::FetchCompleted => {
                    let page = page.ok_or_else(|| worker_error("fetch completed without a result"))?;
                    if screenshot.as_ref().map(Vec::len) != page.screenshot_png_bytes() {
                        return Err(worker_error("incomplete screenshot payload"));
                    }
                    return Ok((page, screenshot));
                }
                WorkerResponse::Error(error) if page.is_none() => return Err(error.into_error()),
                WorkerResponse::Error(_) => {
                    return Err(worker_error("worker returned an error after a partial fetch response"));
                }
                _ => return Err(worker_error("unexpected fetch response")),
            }
        }
    }

    fn crawl(&mut self, request: CrawlWire, events: &Sender<CrawlResult>) -> Result<()> {
        let idle_timeout = crawl_wire_watchdog(&request);
        let absolute_timeout = crawl_wire_absolute_watchdog(&request);
        let deadline = Instant::now() + absolute_timeout;
        let id = self.write_request(WorkerRequest::Crawl(request))?;
        loop {
            match self.recv_response_until(id, idle_timeout, deadline, absolute_timeout, "crawl")? {
                WorkerResponse::CrawlResult(event) => {
                    let result = event.into_result()?;
                    crossbeam_channel::select_biased! {
                        recv(self.lifecycle) -> _ => return Err(cancelled_error()),
                        send(events, result) -> sent => {
                            sent.map_err(|_| worker_error("crawl result receiver closed"))?;
                        },
                    }
                }
                WorkerResponse::CrawlProgress(_) => {}
                WorkerResponse::CrawlCompleted => return Ok(()),
                WorkerResponse::Error(error) => return Err(error.into_error()),
                _ => return Err(worker_error("unexpected crawl response")),
            }
        }
    }

    fn graceful_shutdown(&mut self) -> Result<()> {
        let id = self.write_request(WorkerRequest::Shutdown)?;
        match self.recv_response(id, REAP_GRACE, "shutdown")? {
            WorkerResponse::ShutdownAck => {}
            WorkerResponse::Error(error) => return Err(error.into_error()),
            _ => return Err(worker_error("unexpected shutdown response")),
        }
        self.stdin.take();
        let deadline = Instant::now() + REAP_GRACE;
        loop {
            let child = self.child.as_mut().expect("worker child is present");
            #[cfg(any(unix, windows))]
            let exited = observe_child_exit_without_reaping(child).map_err(worker_error)?;
            #[cfg(not(any(unix, windows)))]
            let exited = false;
            if exited {
                break;
            }
            if Instant::now() >= deadline {
                return Err(worker_error("worker did not exit after shutdown acknowledgement"));
            }
            std::thread::sleep(Duration::from_millis(10));
        }

        let status = self.terminate_and_reap()?;
        if status.success() {
            Ok(())
        } else {
            Err(worker_error(format!("worker exited with {status} after shutdown ack")))
        }
    }

    fn terminate_and_reap(&mut self) -> Result<ExitStatus> {
        if !self.termination_started {
            // Revocation serializes with any in-flight cancellation kill.
            drop(self.authority.revoke());
            let child = self.child.as_ref().expect("worker child is present");
            self.process_tree.terminate(child).map_err(worker_error)?;
            self.termination_started = true;
        }
        if self.exit_status.is_none() {
            let status = self
                .child
                .as_mut()
                .expect("worker child is present")
                .wait()
                .map_err(worker_error)?;
            self.exit_status = Some(status);
        }
        #[cfg(windows)]
        self.process_tree.wait_quiescent().map_err(worker_error)?;
        self.quiescent = true;
        Ok(self.exit_status.expect("reaped worker has an exit status"))
    }

    fn write_request(&mut self, request: WorkerRequest) -> Result<u64> {
        let id = self.next_id;
        self.next_id = self.next_id.wrapping_add(1);
        let frame = RequestFrame { id, request };
        write_bounded_frame(
            self.stdin.as_mut().ok_or_else(|| worker_error("worker stdin closed"))?,
            &frame,
            MAX_WORKER_REQUEST_FRAME_BYTES,
        )?;
        Ok(id)
    }

    fn recv_response(&self, id: u64, timeout: Duration, operation: &'static str) -> Result<WorkerResponse> {
        let deadline = Instant::now() + timeout;
        self.recv_response_until(id, timeout, deadline, timeout, operation)
    }

    fn recv_response_until(
        &self,
        id: u64,
        idle_timeout: Duration,
        absolute_deadline: Instant,
        absolute_timeout: Duration,
        operation: &'static str,
    ) -> Result<WorkerResponse> {
        let bytes = self.recv_frame(idle_timeout, absolute_deadline, absolute_timeout, operation)?;
        let response: ResponseFrame = decode_frame(&bytes)?;
        validate_response(&response, id)?;
        Ok(response.response)
    }

    fn recv_frame(
        &self,
        idle_timeout: Duration,
        absolute_deadline: Instant,
        absolute_timeout: Duration,
        operation: &'static str,
    ) -> Result<Vec<u8>> {
        let remaining = absolute_deadline.saturating_duration_since(Instant::now());
        let Some(wait) = frame_wait_duration(idle_timeout, remaining) else {
            return Err(Error::WorkerProtocolTimeout {
                operation,
                timeout: absolute_timeout,
            });
        };
        let frames = self.frames.as_ref().expect("worker frame receiver is present");
        let timer = crossbeam_channel::after(wait);
        crossbeam_channel::select_biased! {
            recv(self.lifecycle) -> _ => Err(cancelled_error()),
            recv(frames) -> frame => match frame {
                Ok(Ok(frame)) => Ok(frame),
                Ok(Err(error)) => Err(worker_error(error)),
                Err(_) => Err(worker_error("worker transport closed")),
            },
            recv(timer) -> _ => {
                let timeout = if Instant::now() >= absolute_deadline {
                    absolute_timeout
                } else {
                    idle_timeout
                };
                Err(Error::WorkerProtocolTimeout { operation, timeout })
            },
        }
    }

    fn cleanup(&mut self, force: bool) -> Result<()> {
        self.stdin.take();
        #[cfg(unix)]
        self.parent_lifeline.take();

        if !self.quiescent {
            if !force {
                return Err(worker_error(
                    "worker process tree was not quiescent after graceful shutdown",
                ));
            }
            self.terminate_and_reap()?;
        }
        self.child.take();
        self.frames.take();

        let reader = match self.reader.take().map(std::thread::JoinHandle::join) {
            Some(Err(_)) => Err(worker_error("worker stdout reader panicked")),
            _ => Ok(()),
        };
        with_cleanup(reader, self.owner.release())
    }
}

impl Drop for WorkerProcess {
    fn drop(&mut self) {
        if let Err(error) = self.cleanup(true) {
            tracing::error!(%error, "best-effort worker cleanup failed");
        }
    }
}

fn with_cleanup<T>(primary: Result<T>, cleanup: Result<()>) -> Result<T> {
    match (primary, cleanup) {
        (primary, Ok(())) => primary,
        (Ok(_), Err(cleanup)) => Err(cleanup),
        (Err(error), Err(cleanup)) => Err(worker_error(format!("{error}; cleanup failed: {cleanup}"))),
    }
}

#[expect(
    clippy::needless_pass_by_value,
    reason = "the supervisor thread owns command channels and worker configuration for its lifetime"
)]
fn supervisor_thread(
    command: WorkerCommand,
    permit: OwnedSemaphorePermit,
    commands: Receiver<SupervisorCommand>,
    lifecycle: Receiver<Lifecycle>,
    authority: ProcessTreeAuthority,
    bootstrap_result: ResponseSender<PathBuf>,
) -> Result<()> {
    let mut worker = match WorkerProcess::spawn(&command, permit, lifecycle.clone(), authority) {
        Ok(worker) => worker,
        Err(error) => {
            let _ = bootstrap_result.send(Err(error));
            return Ok(());
        }
    };
    if let Err(error) = worker.receive_protocol_info() {
        let result = with_cleanup(Err(error), worker.cleanup(true));
        let _ = bootstrap_result.send(result.map(|()| worker.config_dir()));
        return Ok(());
    }
    if bootstrap_result.send(Ok(worker.config_dir())).is_err() {
        return worker.cleanup(true);
    }

    loop {
        crossbeam_channel::select_biased! {
            recv(lifecycle) -> _ => return worker.cleanup(true),
            recv(commands) -> command => {
                let Ok(command) = command else {
                    return worker.cleanup(true);
                };
                match command {
                    SupervisorCommand::Initialize { request, reply } => match worker.initialize_session(request) {
                        Ok(()) => {
                            let _ = reply.send(Ok(()));
                        }
                        Err(error) => {
                            let _ = reply.send(with_cleanup(Err(error), worker.cleanup(true)));
                            return Ok(());
                        }
                    },
                    SupervisorCommand::Fetch { request, reply } => match worker.fetch(request) {
                        Ok(page) => {
                            let _ = reply.send(Ok(page));
                        }
                        Err(error) => {
                            let terminal = is_terminal_session_error(&error);
                            let result = if terminal {
                                with_cleanup(Err(error), worker.cleanup(true))
                            } else {
                                Err(error)
                            };
                            let _ = reply.send(result);
                            if terminal {
                                return Ok(());
                            }
                        }
                    },
                    SupervisorCommand::Cookies { url, reply } => match worker.cookies(url) {
                        Ok(cookies) => {
                            let _ = reply.send(Ok(cookies));
                        }
                        Err(error) => {
                            let terminal = is_terminal_session_error(&error);
                            let result = if terminal {
                                with_cleanup(Err(error), worker.cleanup(true))
                            } else {
                                Err(error)
                            };
                            let _ = reply.send(result);
                            if terminal {
                                return Ok(());
                            }
                        }
                    },
                    SupervisorCommand::Crawl { request, events, reply } => {
                        let result = worker.crawl(request, &events);
                        drop(events);
                        match result {
                            Ok(()) => {
                                let _ = reply.send(Ok(()));
                            }
                            Err(error) => {
                                let terminal = is_terminal_session_error(&error);
                                let result = if terminal {
                                    with_cleanup(Err(error), worker.cleanup(true))
                                } else {
                                    Err(error)
                                };
                                let _ = reply.send(result);
                                if terminal {
                                    return Ok(());
                                }
                            }
                        }
                    }
                    SupervisorCommand::Shutdown { reply } => {
                        let result = worker.graceful_shutdown();
                        let graceful = result.is_ok();
                        let result = with_cleanup(result, worker.cleanup(!graceful));
                        let _ = reply.send(result);
                        return Ok(());
                    }
                }
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cleanup_failure_is_returned_without_releasing_permit() {
        let permits = Arc::new(tokio::sync::Semaphore::new(1));
        let permit = permits.clone().try_acquire_owned().unwrap();
        let mut owner = SupervisorOwner::new(permit).unwrap();

        let error = owner
            .release_with(|_| Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, "injected")))
            .unwrap_err();
        assert!(error.report().contains("injected"));
        assert!(permits.clone().try_acquire_owned().is_err());

        owner.release().unwrap();
        assert!(permits.try_acquire_owned().is_ok());
    }
}