Skip to content

Commit 15936be

Browse files
authored
Merge pull request #88 from thseiler/fix/add-chromesession-retry-mechanism
fix(main): attempt to resolve macOS CI race condition
2 parents 51da8e3 + 3be5546 commit 15936be

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

html2pdf4doc/main.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pypdf import PdfReader
1919
from requests import Response
2020
from selenium import webdriver
21+
from selenium.common import SessionNotCreatedException
2122
from selenium.webdriver.chrome.options import Options
2223
from selenium.webdriver.chrome.service import Service
2324
from webdriver_manager.core.os_manager import ChromeType, OperationSystemManager
@@ -481,10 +482,30 @@ def create_webdriver(
481482

482483
print("html2pdf4doc: Creating ChromeDriver.", flush=True) # noqa: T201
483484

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+
488509
driver.set_page_load_timeout(page_load_timeout)
489510

490511
print("html2pdf4doc: ChromeDriver created.", flush=True) # noqa: T201

0 commit comments

Comments
 (0)