Skip to content

Fix flaky test_mongo.py::test_simple_ssl file errors in dictionary test infrastructure - #102734

Merged
nikitamikhaylov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/test-mongo-ssl-file-errors
Apr 15, 2026
Merged

nikitamikhaylov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/test-mongo-ssl-file-errors

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Two filesystem bugs in BaseLayoutTester (in test_dictionaries_all_layouts_separate_sources/common.py) caused intermittent FileExistsError and FileNotFoundError crashes in test_mongo.py::test_simple_ssl across all 4 layout variants (flat, hashed, cache, direct).

Root cause

Bug 1 — FileExistsError in cleanup() (8 failures in 30 days):
cleanup() calls shutil.rmtree(path, ignore_errors=True) then os.makedirs(path). When rmtree silently fails to remove the directory (e.g., Docker containers from the previous secure_connection=False parametrization still holding file handles), makedirs() crashes because the directory already exists.

Bug 2 — FileNotFoundError in create_dictionaries() (4 failures in 30 days):
complex_tester and ranged_tester fixtures call create_dictionaries() which writes config files to a directory that only simple_tester.cleanup() creates. Since these three fixtures are independent (no dependency between them), pytest doesn't guarantee resolution order. If ranged_tester resolves before simple_tester, generate_config() → open(path, "w") crashes because the directory doesn't exist yet.

Fix

  1. cleanup(): Add exist_ok=True to os.makedirs() — resilient to failed rmtree
  2. create_dictionaries(): Add os.makedirs(dir, exist_ok=True) at the start — ensures the directory exists regardless of fixture resolution order

CIDB evidence (30-day window)

Date Failures Error Type
2026-04-15 4 FileExistsError
2026-04-11 4 FileNotFoundError

17 distinct PR/master entries total for test_simple_ssl, with 5 master failures. The file errors account for 8 of 22 non-infrastructure failures (the remaining are sanitizer findings and MongoDB connection issues — separate root causes not addressed here).

Verification

All 4 variants pass locally with the fix:

  • ✅ test_simple_ssl[cache-True]
  • ✅ test_simple_ssl[direct-True]
  • ✅ test_simple_ssl[flat-True]
  • ✅ test_simple_ssl[hashed-True]

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

…oundError

Two filesystem bugs in BaseLayoutTester caused intermittent failures in
test_dictionaries_all_layouts_separate_sources/test_mongo.py::test_simple_ssl:

1. cleanup() used os.makedirs() without exist_ok=True. When
   shutil.rmtree(ignore_errors=True) silently failed to remove the
   directory (e.g. Docker containers still holding file handles from
   the previous secure_connection=False parametrization), makedirs()
   crashed with FileExistsError.

2. create_dictionaries() assumed the dictionary config directory already
   existed, but complex_tester and ranged_tester don't call cleanup()
   (which creates the directory). Since pytest doesn't guarantee fixture
   resolution order among independent sibling fixtures, if ranged_tester
   or complex_tester resolved before simple_tester, generate_config()
   crashed with FileNotFoundError.

Fix: add exist_ok=True to os.makedirs() in cleanup(), and ensure the
directory exists at the start of create_dictionaries().
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate (session: cron:clickhouse-ci-task-worker:20260415-061500)

a) Deterministic repro? Yes — the FileExistsError is deterministic when shutil.rmtree fails to remove the directory (directory still exists) and os.makedirs() is called without exist_ok=True. Confirmed with standalone Python test: os.makedirs(existing_dir) → FileExistsError. The FileNotFoundError is deterministic when the dictionary config directory doesn't exist and open(path, "w") is called.

b) Root cause explained? Yes. Two independent bugs: (1) cleanup() calls shutil.rmtree(path, ignore_errors=True) which silently fails when Docker containers hold file handles, then os.makedirs(path) crashes because the directory still exists. This happens specifically in test_mongo.py because the secure_connection parametrization (False→True) causes fixture teardown/recreation. (2) create_dictionaries() assumes the directory exists but doesn't create it. complex_tester and ranged_tester depend on simple_tester.cleanup() having created the directory, but as independent sibling fixtures, pytest doesn't guarantee their resolution order.

c) Fix matches root cause? Yes. exist_ok=True directly addresses the os.makedirs crash when the directory already exists. The os.makedirs call in create_dictionaries() directly addresses the missing directory for independent fixture resolution.

d) Test intent preserved? Yes. The fix only changes test infrastructure (directory creation logic), not the actual test assertions or what is being tested (MongoDB SSL dictionary functionality). No assertions are weakened.

e) Demonstrated in both directions? Old code crashes with FileExistsError/FileNotFoundError (confirmed via standalone Python test simulating both scenarios). New code passes — all 4 variants (cache, direct, flat, hashed) pass locally with the fix applied.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir — could you review this? It fixes two filesystem bugs in the dictionary integration test infrastructure (missing exist_ok=True in os.makedirs) that cause intermittent FileExistsError and FileNotFoundError crashes in test_mongo.py::test_simple_ssl.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Apr 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [035a13b]

Summary: ✅


AI Review

Summary

This PR makes a focused, low-risk test-infrastructure fix in tests/integration/test_dictionaries_all_layouts_separate_sources/common.py by creating dictionary directories with exist_ok=True in both cleanup and create_dictionaries. The change directly addresses flaky FileExistsError/FileNotFoundError behavior caused by fixture ordering and partial cleanup races. I did not find correctness, safety, performance, or ClickHouse-rule violations in the patch.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Apr 15, 2026
@nikitamikhaylov nikitamikhaylov self-assigned this Apr 15, 2026
@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Apr 15, 2026
Merged via the queue into ClickHouse:master with commit 5915bfe Apr 15, 2026
317 of 318 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants