Repository navigation
Commit 59d36e1
authored
fix(connection): convert TLS and unix-socket clients to async (#792)
Closes #789.
## Motivation
`RedisConnectionFactory.sync_to_async_redis` passed the sync pool's
`connection_kwargs` straight into `AsyncConnectionPool` and picked the
TLS class with an exact-type check. A client built with `Redis(ssl=True,
...)`, `Redis(unix_socket_path=...)` or `Sentinel.master_for(...)`
therefore raised `TypeError` on its first async command
(`SemanticCache.acheck`, and `SemanticRouter.aroute` once #784 lands),
and a subclass of `SSLConnection` got a plaintext async connection with
the password still set.
## Changes
- The async connection class is chosen with `issubclass`: TLS
connections (and subclasses) map to `AsyncSSLConnection`, unix-socket
connections to the async `UnixDomainSocketConnection`, everything else
to `AsyncConnection`.
- `_async_connection_kwargs` keeps only the keywords the chosen async
class accepts, found by walking its `__init__` chain
(`_accepted_init_kwargs`), so the filter follows whatever redis-py
version is installed rather than a hard-coded key list. A sync-only
keyword at its default (`None` or `False`) is dropped; one that is set,
such as `ssl_validate_ocsp=True`, raises a `ValueError` naming it,
because the async client cannot honour it and dropping it would silently
weaken TLS verification.
- Sentinel-managed pools are rejected up front with a clear
`ValueError`, as `RedisCluster` already was.
- Seven unit tests in `tests/unit/test_sync_to_async_redis.py`, none
needing a server; five fail on `main`.
## Release Notes
`SemanticCache`, `AsyncSearchIndex` and any other async path that
converts a caller's sync client now work with clients configured with
TLS keywords (`Redis(ssl=True, ...)`), unix sockets, and custom
`SSLConnection` subclasses. Sentinel-managed clients, `RedisCluster`
clients, and clients with a sync-only setting enabled (OCSP validation)
raise `ValueError` at conversion time with a pointer to `redis_url`;
previously they failed with `TypeError` on the first command.
## Reviewer Brief
**Start here:** `redisvl/redis/connection.py`,
`_async_connection_kwargs`. It decides which of the sync pool's settings
reach the async connection and which abort the conversion;
`sync_to_async_redis` only picks the class and calls it.
**Where it could fail**
- `redisvl/redis/connection.py`, `_SYNC_POOL_PLUMBING`: these four
keywords are dropped by name because the sync pool computes them from
the live settings; if a future redis-py adds another pool-computed
keyword, every conversion on a version whose async class rejects it
would raise again. Check against redis-py's
`ConnectionPool._update_connection_kwargs`.
- `redisvl/redis/connection.py`, `_is_unset`:
`maint_notifications_config` is dropped unless `enabled is True`, so a
user who set `enabled="auto"` on purpose gets an async client without
notifications on 7.4/8.0; on 8.1 it passes through.
- `redisvl/redis/connection.py`, `_init_parameters`: stops at the first
`__init__` without `**kwargs`; for the sync side that is the user's
`connection_class`, so a subclass with an explicit-keyword `__init__`
contributes only its own defaults.
**Not verified:** cluster mode (unchanged path, raises as before); a
live TLS or unix-socket server (the tests stop at `make_connection()`,
where every reported failure occurred); the sync `Retry` pass-through at
the end of the function is pre-existing and left for a follow-up.
**Beyond CI:** the ten tests pass on redis-py 7.4.1, 8.0.1 and 8.1.0;
the `resp3` case fails on 7.4.1 and 8.0.1 without the fix, and the
`tls-subclass` case fails on 8.0.1 when either the plumbing filter or
the `maint_notifications_config` rule is removed.
**Since the last brief:** 7c856ed rebases on 0.30.0, drops the
pool-computed keywords, compares sync-only keywords to their own
defaults, trims the `aroute` caveat, and folds the tests into two
parametrised ones.
<sub>Reviewed `7c856ed` · Claude Code (claude-fable-5-1) · lenses:
correctness, security, design, tests, docs</sub>
## Notes
- A set sync-only keyword raises rather than warns. OCSP validation is a
security setting; converting it into a client that cannot validate OCSP,
with only a log line (which #786 shows can be invisible), seemed the
wrong default.
- Sentinel conversion is rejected rather than rebuilt through
`AsyncSentinel`, as the issue suggests. The pool does carry
`sentinel_manager` and `service_name`, so a later change could rebuild
it; that is a separate feature.
- Once this merges, the SSL/unix-socket caveat in #784's
`SemanticRouter` docstring reduces to Sentinel and cluster; I'll trim it
there.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Changes how sync clients are translated into async pools across
index/cache/router async entry points, including TLS and socket
settings; incorrect filtering could misconfigure connections, though
explicit errors protect sync-only security options.
>
> **Overview**
> Fixes **`RedisConnectionFactory.sync_to_async_redis`** so async paths
(`SemanticRouter.aroute`, `AsyncSearchIndex`, semantic caches) can reuse
a caller’s sync client when it uses **TLS**, **TLS subclasses**, or
**unix sockets**, instead of failing on the first command with
**`TypeError`** or building a plaintext async pool.
>
> Conversion now picks the async connection class with **`issubclass`**
(TLS → `AsyncSSLConnection`, unix → `AsyncUnixDomainSocketConnection`),
filters pool kwargs through **`_async_connection_kwargs`**
(version-aware via **`_init_parameters`**, strips RESP3 pool plumbing,
drops sync-only defaults), and **raises `ValueError`** for
**Sentinel-managed** pools and for user-set sync-only TLS options (e.g.
**`ssl_validate_ocsp`**) rather than silently weakening security.
**`SemanticRouter.aroute`** docs now say only **Sentinel** and
**Cluster** need **`redis_url`** for async routing.
>
> Adds **`tests/unit/test_sync_to_async_redis.py`** covering successful
mappings and failure modes without a live Redis server.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
7c856ed. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent f9f2638 commit 59d36e1
3 files changed
Lines changed: 251 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
723 | 723 | | |
724 | 724 | | |
725 | 725 | | |
726 | | - | |
727 | | - | |
| 726 | + | |
| 727 | + | |
728 | 728 | | |
729 | 729 | | |
730 | 730 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
14 | 18 | | |
15 | | - | |
| 19 | + | |
16 | 20 | | |
17 | | - | |
| 21 | + | |
18 | 22 | | |
19 | 23 | | |
20 | 24 | | |
| |||
302 | 306 | | |
303 | 307 | | |
304 | 308 | | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
305 | 402 | | |
306 | 403 | | |
307 | 404 | | |
| |||
876 | 973 | | |
877 | 974 | | |
878 | 975 | | |
879 | | - | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
880 | 991 | | |
881 | 992 | | |
882 | 993 | | |
| |||
885 | 996 | | |
886 | 997 | | |
887 | 998 | | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
888 | 1006 | | |
889 | | - | |
890 | | - | |
891 | | - | |
892 | | - | |
893 | | - | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
894 | 1015 | | |
895 | 1016 | | |
896 | 1017 | | |
897 | 1018 | | |
898 | | - | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
899 | 1022 | | |
900 | 1023 | | |
901 | 1024 | | |
| |||
| 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 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
0 commit comments