Skip to content

fix: avoid config hash crash for non-serializable values - #4241

Merged
johanneskoester merged 3 commits into
snakemake:mainfrom
vetrovk:fix/non-serializable-config-md5
Jul 10, 2026
Merged

johanneskoester merged 3 commits into
snakemake:mainfrom
vetrovk:fix/non-serializable-config-md5

Conversation

@vetrovk

@vetrovk vetrovk commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4215.

Workflow startup logging calculates config_md5 with json.dumps(..., sort_keys=True). This can fail before workflow execution when runtime config contains values that JSON cannot serialize, such as pathlib.Path, or values that produce JSON serialization value errors, such as circular references.

This change preserves the existing deterministic MD5 calculation for JSON-serializable configs. If serialization raises TypeError or ValueError, the startup header reports Config MD5: unavailable instead of crashing. The config object is not mutated, and arbitrary values are not stringified with default=str.

Validation:

  • env XDG_CACHE_HOME="$PWD/.cache" PYTHONPATH=src .venv/bin/python3.12 -m pytest tests/tests.py::test_config tests/tests.py::test_config_non_json_serializable -q
  • env XDG_CACHE_HOME="$PWD/.cache" PYTHONPATH=src .venv/bin/python3.12 -m pytest tests/tests.py -k "config" -q
  • .venv/bin/black --check src/snakemake/workflow.py tests/tests.py
  • git diff --check
  • manual reproduction with pathlib.Path config
  • manual reproduction with circular config
  • deterministic MD5 verification for ordinary JSON config

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • The documentation (docs/) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake).
  • I, as a human being, have checked each line of code in this pull request

AI-assistance disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Summary by CodeRabbit

  • Bug Fixes
    • Workflow information no longer fails when configuration contains non–JSON-serializable values.
    • Shows an “unavailable” configuration checksum instead of erroring.
  • Tests
    • Added a new test to cover workflows using non–JSON-serializable configuration inputs.

@vetrovk
vetrovk requested a review from johanneskoester as a code owner July 10, 2026 06:28
@coderabbitai

coderabbitai Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Workflow.info_header now tolerates non-JSON-serializable configuration values by reporting an unavailable configuration hash. A regression test passes a Path object through workflow configuration.

Changes

Configuration hash fallback

Layer / File(s) Summary
Hash fallback and regression test
src/snakemake/workflow.py, tests/tests.py
info_header catches TypeError and ValueError during configuration serialization and uses "unavailable" for config_md5; a test covers configuration containing a Path value.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address #4215 by catching JSON serialization failures and falling back to "unavailable", with a test covering non-serializable config values.
Out of Scope Changes check ✅ Passed The PR stays focused on config MD5 fallback handling and the corresponding test, with no obvious unrelated changes.
Title check ✅ Passed It clearly summarizes the main change: avoiding crashes when hashing non-serializable config values.
Description check ✅ Passed It includes the required summary, validation, QC checklist, and AI-assistance disclosure sections from the template.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/snakemake/workflow.py`:
- Around line 283-288: In the info_header configuration hash logic, replace the
undefined config reference in the json.dumps call with self.config, while
preserving the existing MD5 calculation and TypeError fallback to "unavailable".
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f8d5e3ac-e6ea-4511-82d3-ef007e1d343f

📥 Commits

Reviewing files that changed from the base of the PR and between c7d1f28 and 94099c7.

📒 Files selected for processing (2)
  • src/snakemake/workflow.py
  • tests/tests.py

Comment thread src/snakemake/workflow.py Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/snakemake/workflow.py (1)

283-288: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Consider also catching ValueError for circular references.

json.dumps raises ValueError (not TypeError) when it encounters circular references in the config. If that occurs, the workflow would still fail to start — the same failure mode this PR aims to prevent. Catching except (TypeError, ValueError) would broaden the graceful fallback to cover that edge case with no additional cost.

This is optional and beyond the stated scope of issue #4215, which specifically mentions TypeError.

♻️ Optional refactor
         try:
             config_md5 = hashlib.md5(
                 json.dumps(self.config, sort_keys=True).encode("utf-8")
             ).hexdigest()
-        except TypeError:
+        except (TypeError, ValueError):
             config_md5 = "unavailable"
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/snakemake/workflow.py` around lines 283 - 288, In the config hash
computation within the workflow initialization logic, broaden the existing
TypeError handler around json.dumps to also catch ValueError, so circular config
references use the "unavailable" fallback instead of aborting startup.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/snakemake/workflow.py`:
- Around line 283-288: In the config hash computation within the workflow
initialization logic, broaden the existing TypeError handler around json.dumps
to also catch ValueError, so circular config references use the "unavailable"
fallback instead of aborting startup.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5ab6d4f0-f05c-4fe9-bb69-027b376bc261

📥 Commits

Reviewing files that changed from the base of the PR and between 94099c7 and c3a54d9.

📒 Files selected for processing (1)
  • src/snakemake/workflow.py

@vetrovk vetrovk changed the title Avoid config hash crash for non-serializable values fix: avoid config hash crash for non-serializable values Jul 10, 2026
@vetrovk

vetrovk commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion. I verified that circular references raise ValueError during JSON serialization and extended the existing fallback to handle both TypeError and ValueError.

The fallback remains limited to config hash generation, and JSON-serializable configs keep the existing deterministic MD5 behavior.

@johanneskoester johanneskoester left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, good catch!

@johanneskoester
johanneskoester enabled auto-merge (squash) July 10, 2026 12:03
@johanneskoester
johanneskoester merged commit 1a27746 into snakemake:main Jul 10, 2026
80 of 89 checks passed
twillis209 pushed a commit to twillis209/snakemake that referenced this pull request Jul 16, 2026
)

Fixes snakemake#4215.

Workflow startup logging calculates `config_md5` with `json.dumps(...,
sort_keys=True)`. This can fail before workflow execution when runtime
config contains values that JSON cannot serialize, such as
`pathlib.Path`, or values that produce JSON serialization value errors,
such as circular references.

This change preserves the existing deterministic MD5 calculation for
JSON-serializable configs. If serialization raises `TypeError` or
`ValueError`, the startup header reports `Config MD5: unavailable`
instead of crashing. The config object is not mutated, and arbitrary
values are not stringified with `default=str`.

Validation:
- `env XDG_CACHE_HOME="$PWD/.cache" PYTHONPATH=src .venv/bin/python3.12
-m pytest tests/tests.py::test_config
tests/tests.py::test_config_non_json_serializable -q`
- `env XDG_CACHE_HOME="$PWD/.cache" PYTHONPATH=src .venv/bin/python3.12
-m pytest tests/tests.py -k "config" -q`
- `.venv/bin/black --check src/snakemake/workflow.py tests/tests.py`
- `git diff --check`
- manual reproduction with `pathlib.Path` config
- manual reproduction with circular config
- deterministic MD5 verification for ordinary JSON config

### QC
<!-- Make sure that you can tick the boxes below. -->

* [x] The PR contains a test case for the changes or the changes are
already covered by an existing test case.
* [x] The documentation (`docs/`) is updated to reflect the changes or
this is not necessary (e.g. if the change does neither modify the
language nor the behavior or functionalities of Snakemake).
* [x] I, as a human being, have checked each line of code in this pull
request

### AI-assistance disclosure
<!--
If AI tools were involved in creating this PR, please check all boxes
that apply
below and make sure that you adhere to our AI-assisted contributions
policy:

https://github.com/snakemake/snakemake/blob/main/docs/project_info/contributing.rst
-->
I used AI assistance for:
* [x] Code generation (e.g., when writing an implementation or fixing a
bug)
* [x] Test/benchmark generation
* [ ] Documentation (including examples)
* [x] Research and understanding

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Workflow information no longer fails when configuration contains
non–JSON-serializable values.
  * Shows an “unavailable” configuration checksum instead of erroring.
* **Tests**
* Added a new test to cover workflows using non–JSON-serializable
configuration inputs.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
johanneskoester pushed a commit that referenced this pull request Jul 30, 2026
🤖 I have created a release *beep* *boop*
---


##
[9.24.0](v9.23.1...v9.24.0)
(2026-07-30)


### Features

* allow to combine cases with otherwise in branch function
([#4262](#4262))
([000ff52](000ff52))


### Bug Fixes

* avoid config hash crash for non-serializable values
([#4241](#4241))
([1a27746](1a27746))
* compilation error when final line contains an f-string
([#4234](#4234))
([9c5e7fe](9c5e7fe))
* Group error log formatting
([#4256](#4256))
([bb6dcde](bb6dcde))
* leak of benchmark monitor threads
([#4259](#4259))
([d37852e](d37852e))
* no benchmark warning when a process exits before it can be sampled
([#4260](#4260))
([6738359](6738359))
* persistence starttime unset/defaults
([#4251](#4251))
([56395c0](56395c0))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression after PR #4079: TypeError: Object of type PosixPath is not JSON serializable

2 participants