Skip to content

Commit 54fbc5f

Browse files
committed
fix(main): attempt to resolve macOS CI race condition
When running sequential PDF exports, macOS CI runners frequently throw a `SessionNotCreatedException` during driver creation. I noticed that subseqeunt tests run without issue. Hypothesis: The OS kernel requires additional time to release TCP ports and IPC locks from the previous headless Chrome instance before a new one can successfully bind. This commit attempts to fix the flakiness by introducing a 3-attempt retry loop with a 1-second delay in-between attempts.
1 parent 51da8e3 commit 54fbc5f

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

html2pdf4doc/main.py

Lines changed: 19 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,24 @@ 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+
driver = None
486+
for attempt in range(3):
487+
try:
488+
driver = webdriver.Chrome(
489+
options=webdriver_options,
490+
service=service,
491+
)
492+
break # Success!
493+
except SessionNotCreatedException:
494+
if attempt == 2:
495+
raise # Out of retries
496+
print( # noqa: T201
497+
"html2pdf4doc: Caught SessionNotCreatedException. Retrying in 1s...",
498+
flush=True,
499+
)
500+
sleep(1.0)
501+
assert driver is not None
502+
488503
driver.set_page_load_timeout(page_load_timeout)
489504

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

0 commit comments

Comments
 (0)