FIX: exception pickle/unpickle round-trip#616
Merged
Conversation
Add __reduce__ to the base Exception class and ConnectionStringParseError so that pickle.loads(pickle.dumps(exc)) reconstructs the exception with the correct constructor arguments. Previously, super().__init__(self.message) stored only one combined string in self.args, but __init__ requires two positional arguments (driver_error, ddbc_error). This caused TypeError on unpickle, breaking multiprocessing, concurrent.futures, celery, and copy.deepcopy. Fixes #587
Contributor
There was a problem hiding this comment.
Pull request overview
Improves exception serialization in mssql_python so driver-defined DB-API exceptions (and ConnectionStringParseError) can be pickled/unpickled reliably—important for multiprocessing and cross-process error propagation.
Changes:
- Added
__reduce__implementations forConnectionStringParseErrorand the DB-API exception base class to support pickle/unpickle round-trips. - Added tests verifying pickle round-trip behavior (and
copy.deepcopy) for DB-API exception subclasses andConnectionStringParseError.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
mssql_python/exceptions.py |
Adds __reduce__ hooks to make custom exceptions pickle/unpickle friendly. |
tests/test_006_exceptions.py |
Adds coverage ensuring exceptions survive pickle/unpickle and deepcopy round-trips. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
📋 Files Needing Attention📉 Files with overall lowest coverage (click to expand)mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.pybind.ddbc_bindings.h: 59.7%
mssql_python.pybind.logger_bridge.hpp: 70.8%
mssql_python.pybind.ddbc_bindings.cpp: 76.1%
mssql_python.row.py: 76.9%
mssql_python.__init__.py: 77.3%
mssql_python.pybind.connection.connection.cpp: 77.3%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.logging.py: 85.5%
mssql_python.connection.py: 85.6%🔗 Quick Links
|
… jahnvi/fix-exception-pickle-587
sumitmsft
approved these changes
Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Work Item / Issue Reference
Summary
This pull request improves the serialization support for custom exception classes in the codebase, specifically ensuring that all DB-API exception subclasses can be pickled and unpickled (serialized and deserialized) correctly. It does so by implementing the
__reduce__method for relevant exception classes and by adding comprehensive tests to verify this behavior.Serialization support for exceptions:
__reduce__method toConnectionStringParseErrorand all DB-API exception subclasses to enable correct pickling and unpickling, preserving all relevant attributesTesting improvements:
tests/test_006_exceptions.pyto verify that all DB-API exception subclasses andConnectionStringParseErrorsurvive a pickle/unpickle round-trip, including cases with empty attributes and deep copies.