-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit 688c2b0
Fix async clients safety when used as an async context manager (#3512)
* Fix async safety when Redis client is used as an async context manager
When the async Redis client is used as an async context manager and
called from different corotuines, one coroutine can exit, shutting
down the client's connection pool, while another coroutine is
attempting to use a connection. This results in a connection error,
such as:
redis.exceptions.ConnectionError: Connection closed by server.
Additional locking in `ConnectionPool` resolves the problem but
introduces extreme latency due to the locking. Instead, this PR
implements a shielded counter that increments as callers enter the async
context manager and decrements when they exit. The client then closes
its connection pool only after all active contexts exit.
Performance is on par with use of the client without a context manager.1 parent 349d761 commit 688c2b0
File tree
4 files changed
+111
-5
lines changed- redis/asyncio
- tests/test_asyncio
4 files changed
+111
-5
lines changedLines changed: 4 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
81 | 81 |
| |
82 | 82 |
| |
83 | 83 |
| |
84 | + | ||
85 | + | ||
86 | + | ||
87 | + | ||
84 | 88 |
| |
85 | 89 |
| |
86 | 90 |
| |
|
Lines changed: 45 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
387 | 387 |
| |
388 | 388 |
| |
389 | 389 |
| |
390 | + | ||
391 | + | ||
392 | + | ||
393 | + | ||
394 | + | ||
395 | + | ||
390 | 396 |
| |
391 | 397 |
| |
392 | 398 |
| |
| |||
594 | 600 |
| |
595 | 601 |
| |
596 | 602 |
| |
597 | - | ||
603 | + | ||
604 | + | ||
605 | + | ||
606 | + | ||
607 | + | ||
608 | + | ||
609 | + | ||
610 | + | ||
611 | + | ||
612 | + | ||
613 | + | ||
614 | + | ||
615 | + | ||
616 | + | ||
617 | + | ||
618 | + | ||
619 | + | ||
620 | + | ||
621 | + | ||
622 | + | ||
623 | + | ||
624 | + | ||
625 | + | ||
626 | + | ||
627 | + | ||
628 | + | ||
629 | + | ||
630 | + | ||
631 | + | ||
632 | + | ||
633 | + | ||
598 | 634 |
| |
599 | 635 |
| |
600 | - | ||
636 | + | ||
637 | + | ||
638 | + | ||
639 | + | ||
640 | + | ||
641 | + | ||
642 | + | ||
643 | + | ||
601 | 644 |
| |
602 | 645 |
| |
603 | 646 |
| |
|
Lines changed: 46 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
431 | 431 |
| |
432 | 432 |
| |
433 | 433 |
| |
434 | + | ||
435 | + | ||
436 | + | ||
437 | + | ||
438 | + | ||
439 | + | ||
434 | 440 |
| |
435 | 441 |
| |
436 | 442 |
| |
| |||
467 | 473 |
| |
468 | 474 |
| |
469 | 475 |
| |
470 | - | ||
476 | + | ||
477 | + | ||
478 | + | ||
479 | + | ||
480 | + | ||
481 | + | ||
482 | + | ||
483 | + | ||
484 | + | ||
485 | + | ||
486 | + | ||
487 | + | ||
488 | + | ||
471 | 489 |
| |
472 | - | ||
473 | - | ||
490 | + | ||
491 | + | ||
492 | + | ||
493 | + | ||
494 | + | ||
495 | + | ||
496 | + | ||
497 | + | ||
498 | + | ||
499 | + | ||
500 | + | ||
501 | + | ||
502 | + | ||
503 | + | ||
504 | + | ||
505 | + | ||
506 | + | ||
507 | + | ||
508 | + | ||
509 | + | ||
510 | + | ||
511 | + | ||
512 | + | ||
513 | + | ||
514 | + | ||
515 | + | ||
516 | + | ||
474 | 517 |
| |
475 | 518 |
| |
476 | 519 |
| |
|
Lines changed: 16 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | + | ||
2 | + | ||
3 | + | ||
4 | + | ||
5 | + | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | + | ||
10 | + | ||
11 | + | ||
12 | + | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | + |
0 commit comments