Commit 2ad6852
committed
Fix COPY OUT race conditions and state corruption (Python 3.9.9 specific)
This commit addresses critical concurrency issues in the `COPY OUT` protocol implementation. These issues were specifically observed in Python 3.9.9 environments (likely due to differences in asyncio event loop scheduling compared to newer versions like 3.13), leading to deadlocks and data corruption.
Changes:
1. Fix Data Loss (Backpressure Handling):
- Issue: In Python 3.9, `_dispatch_result` could be triggered multiple times while the sink was paused, overwriting `_pending_result`.
- Fix: Implemented data merging logic. New incoming data is now appended to the existing pending buffer instead of replacing it.
2. Fix State Pollution on Cancel:
- Issue: Cancelling a `COPY` task left residual data in `_pending_result`. Subsequent queries (e.g., `SELECT 1`) would incorrectly consume this stale data, causing `AttributeError: 'tuple' object has no attribute '_init_types'`.
- Fix: Added a `try...finally` block in `copy_out` to unconditionally clear `_pending_result` and reset protocol state upon exit.
3. Fix Deadlocks:
- Issue: The connection could get stuck in a paused state if execution was interrupted or if `resume_reading` was missed.
- Fix: Ensured `self.resume_reading()` is called immediately after consuming buffered data in `_new_waiter` and in the cleanup phase.
4. Fix Logic Errors:
- corrected `_new_waiter` to prevent assigning completed Futures to `self.waiter`, avoiding `InternalClientError`.
Fixes #81 parent 161e685 commit 2ad6852
2 files changed
+40
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
| 106 | + | |
105 | 107 | | |
106 | 108 | | |
107 | 109 | | |
| |||
414 | 416 | | |
415 | 417 | | |
416 | 418 | | |
417 | | - | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
418 | 423 | | |
419 | 424 | | |
420 | 425 | | |
| |||
424 | 429 | | |
425 | 430 | | |
426 | 431 | | |
| 432 | + | |
427 | 433 | | |
428 | 434 | | |
429 | 435 | | |
| |||
776 | 782 | | |
777 | 783 | | |
778 | 784 | | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
779 | 793 | | |
780 | 794 | | |
781 | 795 | | |
| |||
848 | 862 | | |
849 | 863 | | |
850 | 864 | | |
851 | | - | |
852 | | - | |
| 865 | + | |
| 866 | + | |
853 | 867 | | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
854 | 886 | | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
855 | 890 | | |
856 | 891 | | |
857 | 892 | | |
| |||
0 commit comments