mineru 0.2.8

Rust client, CLI, and API server for OpenAI-compatible MinerU document parsing
Documentation
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
//! Bounded, path-free conversion of declared document bytes into a PDF.
use crate::{OfficeWorkers, OfficialPdfOptions, mineru_api::ooxml};
use bytes::Bytes;
use hayro_jpeg2000::{DecodeSettings, Image as Jp2Image};
use image::{AnimationDecoder, DynamicImage, ImageDecoder, ImageFormat, ImageReader};
use lopdf::{Document, Object, Stream, dictionary};
use std::{
    io::{BufReader, Cursor},
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};
use tokio::{
    sync::{Mutex as AsyncMutex, oneshot},
    task::JoinHandle,
};

#[derive(Clone)]
pub struct RasterWorkers {
    state: Arc<Mutex<RasterWorkerState>>,
    drain_gate: Arc<AsyncMutex<()>>,
}

impl Default for RasterWorkers {
    fn default() -> Self {
        Self {
            state: Arc::default(),
            drain_gate: Arc::default(),
        }
    }
}

#[derive(Default)]
struct RasterWorkerState {
    draining: bool,
    owners: Vec<JoinHandle<()>>,
}

impl RasterWorkers {
    async fn reap(&self) {
        let owners = {
            let mut state = self.state.lock().expect("raster worker registry poisoned");
            let mut owners = Vec::new();
            let mut i = 0;
            while i < state.owners.len() {
                if state.owners[i].is_finished() {
                    owners.push(state.owners.swap_remove(i));
                } else {
                    i += 1;
                }
            }
            owners
        };
        for owner in owners {
            let _ = owner.await;
        }
    }

    async fn submit<T: Send + 'static>(
        &self,
        job: impl FnOnce() -> Result<T, String> + Send + 'static,
    ) -> Result<T, String> {
        self.reap().await;
        let (send, receive) = oneshot::channel();
        {
            let mut state = self.state.lock().expect("raster worker registry poisoned");
            if state.draining {
                return Err("image preparation workers are draining".into());
            }
            let owner = tokio::spawn(async move {
                let result = tokio::task::spawn_blocking(job)
                    .await
                    .map_err(|_| "image preparation worker stopped".to_string())
                    .and_then(|result| result);
                let _ = send.send(result);
            });
            state.owners.push(owner);
        }
        receive
            .await
            .map_err(|_| "image preparation worker stopped".to_string())?
    }

    #[cfg(test)]
    pub(crate) async fn test_admission(&self) -> Result<(), String> {
        self.submit(|| Ok(())).await
    }

    pub async fn drain(&self) {
        let _drain = self.drain_gate.lock().await;
        let owners = {
            let mut state = self.state.lock().expect("raster worker registry poisoned");
            state.draining = true;
            std::mem::take(&mut state.owners)
        };
        for owner in owners {
            let _ = owner.await;
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DocumentKind {
    Pdf,
    Png,
    Jpeg,
    Jpg,
    Jp2,
    Webp,
    Gif,
    Bmp,
    Tiff,
    Docx,
    Pptx,
    Xlsx,
}
impl DocumentKind {
    /// Returns the closed set of input types accepted by the local runners.
    pub fn from_suffix(suffix: &str) -> Option<Self> {
        if suffix.eq_ignore_ascii_case("pdf") {
            Some(Self::Pdf)
        } else if suffix.eq_ignore_ascii_case("png") {
            Some(Self::Png)
        } else if suffix.eq_ignore_ascii_case("jpeg") {
            Some(Self::Jpeg)
        } else if suffix.eq_ignore_ascii_case("jpg") {
            Some(Self::Jpg)
        } else if suffix.eq_ignore_ascii_case("jp2") {
            Some(Self::Jp2)
        } else if suffix.eq_ignore_ascii_case("webp") {
            Some(Self::Webp)
        } else if suffix.eq_ignore_ascii_case("gif") {
            Some(Self::Gif)
        } else if suffix.eq_ignore_ascii_case("bmp") {
            Some(Self::Bmp)
        } else if suffix.eq_ignore_ascii_case("tiff") {
            Some(Self::Tiff)
        } else if suffix.eq_ignore_ascii_case("docx") {
            Some(Self::Docx)
        } else if suffix.eq_ignore_ascii_case("pptx") {
            Some(Self::Pptx)
        } else if suffix.eq_ignore_ascii_case("xlsx") {
            Some(Self::Xlsx)
        } else {
            None
        }
    }
    pub const fn suffix(self) -> &'static str {
        match self {
            Self::Pdf => "pdf",
            Self::Png => "png",
            Self::Jpeg => "jpeg",
            Self::Jpg => "jpg",
            Self::Jp2 => "jp2",
            Self::Webp => "webp",
            Self::Gif => "gif",
            Self::Bmp => "bmp",
            Self::Tiff => "tiff",
            Self::Docx => "docx",
            Self::Pptx => "pptx",
            Self::Xlsx => "xlsx",
        }
    }
    pub const fn is_office(self) -> bool {
        matches!(self, Self::Docx | Self::Pptx | Self::Xlsx)
    }
    pub const fn supports_page_range(self) -> bool {
        matches!(self, Self::Pdf)
    }
}

#[derive(Debug)]
pub struct PreparedPdf {
    pub bytes: Bytes,
    pub kind: DocumentKind,
    pub original: Bytes,
}

pub async fn prepare(
    bytes: impl Into<Bytes>,
    declared: DocumentKind,
    options: &OfficialPdfOptions,
    workers: &OfficeWorkers,
    raster_workers: &RasterWorkers,
    remaining: Duration,
) -> Result<PreparedPdf, String> {
    prepare_with_warning(bytes, declared, options, workers, raster_workers, remaining)
        .await
        .map(|(prepared, _)| prepared)
}

#[doc(hidden)]
pub async fn prepare_with_warning(
    bytes: impl Into<Bytes>,
    declared: DocumentKind,
    options: &OfficialPdfOptions,
    workers: &OfficeWorkers,
    raster_workers: &RasterWorkers,
    remaining: Duration,
) -> Result<(PreparedPdf, Option<String>), String> {
    prepare_with_warning_and_ooxml(
        bytes,
        declared,
        options,
        workers,
        raster_workers,
        remaining,
        crate::command::service::OoxmlLimits::default_resolved(),
    )
    .await
}

/// Crate-private variant carrying the frozen Phase-1B OOXML preflight policy.
pub(crate) async fn prepare_with_warning_and_ooxml(
    bytes: impl Into<Bytes>,
    declared: DocumentKind,
    options: &OfficialPdfOptions,
    workers: &OfficeWorkers,
    raster_workers: &RasterWorkers,
    remaining: Duration,
    ooxml: crate::command::service::OoxmlLimits,
) -> Result<(PreparedPdf, Option<String>), String> {
    let bytes = bytes.into();
    let original = bytes.clone();
    let deadline = Instant::now()
        .checked_add(remaining)
        .ok_or("input preparation deadline overflow")?;
    let mut validation_options = options.clone();
    if declared != DocumentKind::Pdf {
        validation_options.start_page = 0;
        validation_options.end_page = None;
    }
    validation_options.validate().map_err(|e| e.to_string())?;
    if bytes.len() > options.max_pdf_bytes {
        return Err(format!(
            "input exceeds PDF byte limit ({} bytes; limit {} bytes); raise with --max-pdf-bytes or MINERU_MAX_PDF_BYTES",
            bytes.len(),
            options.max_pdf_bytes
        ));
    }
    if remaining.is_zero() {
        return Err("input preparation deadline expired".into());
    }
    if declared == DocumentKind::Pdf {
        if !bytes.starts_with(b"%PDF-") {
            return Err("suffix/content mismatch".into());
        }
        deadline_expired(deadline)?;
        return Ok((
            PreparedPdf {
                bytes: bytes.clone(),
                kind: declared,
                original,
            },
            None,
        ));
    }
    if declared.is_office() {
        let (bytes, detected) = tokio::task::spawn_blocking(move || {
            let detected = ooxml::detect_bytes_with_limits(&bytes, ooxml);
            (bytes, detected)
        })
        .await
        .map_err(|_| "input preparation worker stopped".to_string())?;
        let found = detected
            .map_err(|e| format!("invalid OOXML: {e}"))?
            .ok_or("invalid OOXML")?;
        let kind = match found {
            "docx" => DocumentKind::Docx,
            "pptx" => DocumentKind::Pptx,
            "xlsx" => DocumentKind::Xlsx,
            _ => return Err("invalid OOXML".into()),
        };
        if kind != declared {
            return Err("suffix/content mismatch".into());
        }
        let timeout = deadline
            .checked_duration_since(Instant::now())
            .filter(|timeout| !timeout.is_zero())
            .ok_or("input preparation deadline expired")?;
        let (pdf, warning) = workers
            .convert_with_warning(found, bytes, timeout)
            .await
            .map_err(|e| e.to_string())?;
        validate_prepared_pdf(&pdf, options)?;
        deadline_expired(deadline)?;
        return Ok((
            PreparedPdf {
                bytes: pdf.into(),
                kind,
                original,
            },
            warning,
        ));
    }
    let options = options.clone();
    let pdf = raster_workers
        .submit(move || {
            if declared == DocumentKind::Jp2 {
                jp2_pdf(bytes, &options)
            } else {
                image_pdf(
                    bytes,
                    format_for(declared).ok_or("unsupported document kind")?,
                    &options,
                )
            }
        })
        .await?;
    deadline_expired(deadline)?;
    Ok((
        PreparedPdf {
            bytes: pdf.into(),
            kind: declared,
            original,
        },
        None,
    ))
}

fn deadline_expired(deadline: Instant) -> Result<(), String> {
    if Instant::now() >= deadline {
        Err("input preparation deadline expired".into())
    } else {
        Ok(())
    }
}

fn format_for(kind: DocumentKind) -> Option<ImageFormat> {
    Some(match kind {
        DocumentKind::Png => ImageFormat::Png,
        DocumentKind::Jpeg | DocumentKind::Jpg => ImageFormat::Jpeg,
        DocumentKind::Webp => ImageFormat::WebP,
        DocumentKind::Gif => ImageFormat::Gif,
        DocumentKind::Bmp => ImageFormat::Bmp,
        DocumentKind::Tiff => ImageFormat::Tiff,
        _ => return None,
    })
}
fn image_pdf(
    bytes: Bytes,
    expected: ImageFormat,
    options: &OfficialPdfOptions,
) -> Result<Vec<u8>, String> {
    let reader = ImageReader::with_format(Cursor::new(&bytes), expected);
    let mut decoder = reader.into_decoder().map_err(|_| "invalid image")?;
    let (w, h) = decoder.dimensions();
    limits(w, h, options)?;
    reject_animation(&bytes, expected)?;
    let orientation = decoder.orientation().map_err(|_| "invalid image")?;
    let mut image = DynamicImage::from_decoder(decoder).map_err(|_| "invalid image")?;
    image.apply_orientation(orientation);
    let rgb = white_rgb(image);
    limits(rgb.width(), rgb.height(), options)?;
    pdf_from_rgb(rgb.width(), rgb.height(), rgb.into_raw(), options)
}
fn jp2_pdf(bytes: Bytes, options: &OfficialPdfOptions) -> Result<Vec<u8>, String> {
    let image = Jp2Image::new(&bytes, &DecodeSettings::default()).map_err(|_| "invalid JP2")?;
    let (w, h) = (image.width(), image.height());
    limits(w, h, options)?;
    // Image::new validates the JP2/J2C container and codestream header; JPXDecode
    // retains the validated source bytes without allocating a second raster.
    pdf_from_jpx(w, h, bytes.to_vec(), options)
}
fn limits(w: u32, h: u32, o: &OfficialPdfOptions) -> Result<(), String> {
    let px = (w as u64)
        .checked_mul(h as u64)
        .ok_or("image dimensions overflow")?;
    let rgb = px.checked_mul(3).ok_or("image dimensions overflow")?;
    if w > u16::MAX as u32
        || h > u16::MAX as u32
        || px > o.max_page_pixels
        || rgb > o.max_rendered_image_bytes.min(o.max_in_flight_image_bytes) as u64
    {
        Err("image exceeds limits".into())
    } else {
        Ok(())
    }
}
fn white_rgb(image: DynamicImage) -> image::RgbImage {
    let rgba = image.to_rgba8();
    image::RgbImage::from_fn(rgba.width(), rgba.height(), |x, y| {
        let p = rgba.get_pixel(x, y);
        let a = p[3] as u16;
        image::Rgb([
            ((p[0] as u16 * a + 255 * (255 - a)) / 255) as u8,
            ((p[1] as u16 * a + 255 * (255 - a)) / 255) as u8,
            ((p[2] as u16 * a + 255 * (255 - a)) / 255) as u8,
        ])
    })
}
fn pdf_from_rgb(w: u32, h: u32, data: Vec<u8>, o: &OfficialPdfOptions) -> Result<Vec<u8>, String> {
    let mut doc = Document::with_version("1.5");
    let mut stream = Stream::new(
        dictionary! {"Type"=>"XObject","Subtype"=>"Image","Width"=>w as i64,"Height"=>h as i64,"ColorSpace"=>"DeviceRGB","BitsPerComponent"=>8},
        data,
    );
    stream.compress().map_err(|_| "image compression failed")?;
    let image = doc.add_object(stream);
    finish_image_pdf(&mut doc, image, w, h, o)
}
fn pdf_from_jpx(w: u32, h: u32, data: Vec<u8>, o: &OfficialPdfOptions) -> Result<Vec<u8>, String> {
    let mut doc = Document::with_version("1.5");
    // JPXDecode carries its own colorspace and component metadata.
    let image = doc.add_object(Stream::new(dictionary!{"Type"=>"XObject","Subtype"=>"Image","Width"=>w as i64,"Height"=>h as i64,"Filter"=>"JPXDecode"}, data));
    finish_image_pdf(&mut doc, image, w, h, o)
}
fn finish_image_pdf(
    doc: &mut Document,
    image: lopdf::ObjectId,
    w: u32,
    h: u32,
    o: &OfficialPdfOptions,
) -> Result<Vec<u8>, String> {
    let content = doc.add_object(Stream::new(
        dictionary! {},
        format!(
            "q {} 0 0 {} 0 0 cm /Im0 Do Q",
            w as f64 * 72.0 / 200.0,
            h as f64 * 72.0 / 200.0
        )
        .into_bytes(),
    ));
    let page=doc.add_object(dictionary!{"Type"=>"Page","MediaBox"=>vec![0.into(),0.into(),(w as f64*72./200.).into(),(h as f64*72./200.).into()],"Resources"=>dictionary!{"XObject"=>dictionary!{"Im0"=>image}},"Contents"=>content});
    let pages = doc.new_object_id();
    doc.objects.insert(
        pages,
        Object::Dictionary(dictionary! {"Type"=>"Pages","Kids"=>vec![page.into()],"Count"=>1}),
    );
    doc.get_object_mut(page)
        .map_err(|_| "PDF construction failed")?
        .as_dict_mut()
        .map_err(|_| "PDF construction failed")?
        .set("Parent", pages);
    let catalog = doc.add_object(dictionary! {"Type"=>"Catalog","Pages"=>pages});
    doc.trailer.set("Root", catalog);
    let mut out = Vec::new();
    doc.save_to(&mut out)
        .map_err(|_| "PDF construction failed")?;
    validate_image_pdf(&out, o)?;
    Ok(out)
}
fn validate_prepared_pdf(bytes: &[u8], o: &OfficialPdfOptions) -> Result<(), String> {
    if bytes.len() > o.max_pdf_bytes {
        return Err(format!(
            "PDF exceeds size limit ({} bytes; limit {} bytes); raise with --max-pdf-bytes or MINERU_MAX_PDF_BYTES",
            bytes.len(),
            o.max_pdf_bytes
        ));
    }
    let doc = Document::load_mem(bytes).map_err(|_| "invalid generated PDF")?;
    if doc.is_encrypted() || doc.get_pages().is_empty() || doc.get_pages().len() > o.max_pages {
        return Err("PDF page limit exceeded".into());
    }
    Ok(())
}
fn validate_image_pdf(bytes: &[u8], o: &OfficialPdfOptions) -> Result<(), String> {
    validate_prepared_pdf(bytes, o)?;
    (Document::load_mem(bytes)
        .map_err(|_| "invalid generated PDF")?
        .get_pages()
        .len()
        == 1)
        .then_some(())
        .ok_or("generated PDF is not one page".into())
}
fn reject_animation(bytes: &[u8], f: ImageFormat) -> Result<(), String> {
    match f {
        ImageFormat::Png => image::codecs::png::PngDecoder::new(BufReader::new(Cursor::new(bytes)))
            .map_err(|_| "invalid image")?
            .is_apng()
            .map_err(|_| "invalid image")?
            .then_some(()),
        ImageFormat::WebP => image::codecs::webp::WebPDecoder::new(Cursor::new(bytes))
            .map_err(|_| "invalid image")?
            .has_animation()
            .then_some(()),
        ImageFormat::Gif => {
            let mut frames = image::codecs::gif::GifDecoder::new(Cursor::new(bytes))
                .map_err(|_| "invalid image")?
                .into_frames();
            frames.next().transpose().map_err(|_| "invalid image")?;
            frames
                .next()
                .transpose()
                .map_err(|_| "invalid image")?
                .is_some()
                .then_some(())
        }
        ImageFormat::Tiff => tiff::decoder::Decoder::new(Cursor::new(bytes))
            .map_err(|_| "invalid image")?
            .more_images()
            .then_some(()),
        _ => None,
    }
    .map_or(Ok(()), |_| {
        Err("animated or multi-page images are unsupported".into())
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, Ordering};

    #[test]
    fn document_kind_suffixes_are_closed_and_case_insensitive() {
        let all = [
            DocumentKind::Pdf,
            DocumentKind::Png,
            DocumentKind::Jpeg,
            DocumentKind::Jpg,
            DocumentKind::Jp2,
            DocumentKind::Webp,
            DocumentKind::Gif,
            DocumentKind::Bmp,
            DocumentKind::Tiff,
            DocumentKind::Docx,
            DocumentKind::Pptx,
            DocumentKind::Xlsx,
        ];
        for kind in all {
            assert_eq!(DocumentKind::from_suffix(kind.suffix()), Some(kind));
            assert_eq!(
                DocumentKind::from_suffix(&kind.suffix().to_ascii_uppercase()),
                Some(kind)
            );
        }
        assert_eq!(DocumentKind::from_suffix("txt"), None);
    }

    #[test]
    fn deadline_is_expired_at_the_boundary() {
        assert_eq!(
            deadline_expired(Instant::now()).unwrap_err(),
            "input preparation deadline expired"
        );
    }

    #[tokio::test]
    async fn non_office_preparation_has_no_warning() {
        let workers = OfficeWorkers::with_executable("unused".into());
        let (prepared, warning) = prepare_with_warning(
            Bytes::from_static(b"%PDF-1.4"),
            DocumentKind::Pdf,
            &OfficialPdfOptions::default(),
            &workers,
            &RasterWorkers::default(),
            Duration::from_secs(1),
        )
        .await
        .unwrap();
        assert_eq!(prepared.kind, DocumentKind::Pdf);
        assert_eq!(warning, None);
    }

    #[tokio::test]
    async fn pdf_byte_limit_error_names_the_raise_knob() {
        let workers = OfficeWorkers::with_executable("unused".into());
        let mut options = OfficialPdfOptions::default();
        options.max_pdf_bytes = 4;
        let error = prepare_with_warning(
            Bytes::from_static(b"%PDF-1.4"),
            DocumentKind::Pdf,
            &options,
            &workers,
            &RasterWorkers::default(),
            Duration::from_secs(1),
        )
        .await
        .unwrap_err();
        assert!(error.contains("input exceeds PDF byte limit"));
        assert!(error.contains("--max-pdf-bytes") && error.contains("MINERU_MAX_PDF_BYTES"));
    }

    #[test]
    fn prepared_pdf_size_error_names_the_raise_knob() {
        let mut options = OfficialPdfOptions::default();
        options.max_pdf_bytes = 4;
        let error = validate_prepared_pdf(b"0123456789", &options).unwrap_err();
        assert!(error.contains("PDF exceeds size limit"));
        assert!(error.contains("--max-pdf-bytes") && error.contains("MINERU_MAX_PDF_BYTES"));
    }

    #[tokio::test]
    async fn raster_owner_survives_caller_abort_and_drain_closes_admission() {
        let workers = RasterWorkers::default();
        let (started_send, started) = oneshot::channel();
        let (release_send, release) = std::sync::mpsc::channel();
        let done = Arc::new(AtomicBool::new(false));
        let job_workers = workers.clone();
        let job_done = done.clone();
        let caller = tokio::spawn(async move {
            job_workers
                .submit(move || {
                    let _ = started_send.send(());
                    release.recv().unwrap();
                    job_done.store(true, Ordering::SeqCst);
                    Ok(())
                })
                .await
        });
        started.await.unwrap();
        caller.abort();
        let _ = caller.await;
        let mut draining = tokio::spawn({
            let workers = workers.clone();
            async move { workers.drain().await }
        });
        assert!(
            tokio::time::timeout(Duration::from_millis(20), &mut draining)
                .await
                .is_err()
        );
        release_send.send(()).unwrap();
        draining.await.unwrap();
        assert!(done.load(Ordering::SeqCst));
        assert_eq!(
            workers.submit(|| Ok(())).await.unwrap_err(),
            "image preparation workers are draining"
        );
    }

    #[tokio::test]
    async fn concurrent_drains_wait_for_the_same_blocked_owner() {
        let workers = RasterWorkers::default();
        let (started_send, started) = oneshot::channel();
        let (release_send, release) = std::sync::mpsc::channel();
        let job_workers = workers.clone();
        let caller = tokio::spawn(async move {
            job_workers
                .submit(move || {
                    let _ = started_send.send(());
                    release.recv().unwrap();
                    Ok(())
                })
                .await
        });
        started.await.unwrap();
        caller.abort();
        let _ = caller.await;

        let mut first = tokio::spawn({
            let workers = workers.clone();
            async move { workers.drain().await }
        });
        let mut second = tokio::spawn({
            let workers = workers.clone();
            async move { workers.drain().await }
        });
        assert!(
            tokio::time::timeout(Duration::from_millis(20), async {
                tokio::select! {
                    _ = &mut first => (),
                    _ = &mut second => (),
                }
            })
            .await
            .is_err()
        );
        release_send.send(()).unwrap();
        first.await.unwrap();
        second.await.unwrap();
    }
}