Commit bdf8636
Fix race between zlib Inflate/Deflate and Dispose (#128752)
Fixes #128550.
`Inflater`/`Deflater` synchronize their native zlib calls via a managed
`lock`, but `Dispose` is not synchronized, so a concurrent `Dispose` on
one thread could run `InflateEnd`/`DeflateEnd` and free the underlying
`z_stream` while another thread was mid-call inside the native
`inflate`/`deflate`, leading to use-after-free crashes.
The fix moves the protection down into `ZLibStreamHandle`. `Inflate`,
`InflateReset2_`, and `Deflate` now bracket their native invocations
with `DangerousAddRef`/`DangerousRelease`, which is exactly what
`SafeHandle` is designed for: it defers `ReleaseHandle` (and therefore
the `InflateEnd`/`DeflateEnd` P/Invoke) until all in-flight callers have
released their refs. The redundant `EnsureNotDisposed` check is removed
from these methods since `DangerousAddRef` already throws
`ObjectDisposedException` for a closed handle.
The other `ZLibStreamHandle` members (`DeflateEnd`, `InflateEnd`,
`InflateInit2_`, `DeflateInit2_`) are intentionally left as-is: they
only run from initialization or from `ReleaseHandle`/`Dispose` paths
that are not the racing party.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent af7e729 commit bdf8636
1 file changed
Lines changed: 47 additions & 21 deletions
Lines changed: 47 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
288 | 288 | | |
289 | 289 | | |
290 | 290 | | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | 291 | | |
297 | 292 | | |
298 | 293 | | |
| |||
348 | 343 | | |
349 | 344 | | |
350 | 345 | | |
351 | | - | |
352 | | - | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
353 | 351 | | |
354 | | - | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
355 | 358 | | |
356 | | - | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
357 | 363 | | |
358 | 364 | | |
359 | 365 | | |
360 | | - | |
| 366 | + | |
361 | 367 | | |
362 | | - | |
363 | 368 | | |
364 | 369 | | |
365 | 370 | | |
| |||
395 | 400 | | |
396 | 401 | | |
397 | 402 | | |
398 | | - | |
399 | | - | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
400 | 408 | | |
401 | | - | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
402 | 415 | | |
403 | | - | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
404 | 420 | | |
405 | 421 | | |
406 | 422 | | |
407 | 423 | | |
408 | 424 | | |
409 | | - | |
410 | | - | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
411 | 430 | | |
412 | | - | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
413 | 437 | | |
414 | | - | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
415 | 442 | | |
416 | 443 | | |
417 | 444 | | |
418 | | - | |
| 445 | + | |
419 | 446 | | |
420 | | - | |
421 | 447 | | |
422 | 448 | | |
423 | 449 | | |
| |||
0 commit comments