|
18 | 18 | from pypdf import PdfReader |
19 | 19 | from requests import Response |
20 | 20 | from selenium import webdriver |
| 21 | +from selenium.common import SessionNotCreatedException |
21 | 22 | from selenium.webdriver.chrome.options import Options |
22 | 23 | from selenium.webdriver.chrome.service import Service |
23 | 24 | from webdriver_manager.core.os_manager import ChromeType, OperationSystemManager |
@@ -481,10 +482,30 @@ def create_webdriver( |
481 | 482 |
|
482 | 483 | print("html2pdf4doc: Creating ChromeDriver.", flush=True) # noqa: T201 |
483 | 484 |
|
484 | | - driver = webdriver.Chrome( |
485 | | - options=webdriver_options, |
486 | | - service=service, |
487 | | - ) |
| 485 | + # When running sequential PDF exports, macOS CI sometimes throws a |
| 486 | + # SessionNotCreatedException during driver creation. |
| 487 | + # Hypothesis: The OS kernel might need additional time to release TCP ports |
| 488 | + # and IPC locks from the previous headless Chrome instance before a new one |
| 489 | + # can successfully bind. |
| 490 | + # Workaround: Use 3-attempt retry loop with a 1-second delay between attempts. |
| 491 | + driver = None |
| 492 | + for attempt in range(3): |
| 493 | + try: |
| 494 | + driver = webdriver.Chrome( |
| 495 | + options=webdriver_options, |
| 496 | + service=service, |
| 497 | + ) |
| 498 | + break # Success! |
| 499 | + except SessionNotCreatedException: |
| 500 | + if attempt == 2: |
| 501 | + raise # Out of retries |
| 502 | + print( # noqa: T201 |
| 503 | + "html2pdf4doc: Caught SessionNotCreatedException. Retrying in 1s...", |
| 504 | + flush=True, |
| 505 | + ) |
| 506 | + sleep(1.0) |
| 507 | + assert driver is not None |
| 508 | + |
488 | 509 | driver.set_page_load_timeout(page_load_timeout) |
489 | 510 |
|
490 | 511 | print("html2pdf4doc: ChromeDriver created.", flush=True) # noqa: T201 |
|
0 commit comments