Skip to content

[Bugfix][Router] Keep routing settings across a dynamic config reload - #1167

Open
rahulssv-ibm wants to merge 3 commits into
vllm-project:mainfrom
rahulssv-ibm:fix/dynamic-config-routing-args
Open

rahulssv-ibm wants to merge 3 commits into
vllm-project:mainfrom
rahulssv-ibm:fix/dynamic-config-routing-args

Conversation

@rahulssv-ibm

@rahulssv-ibm rahulssv-ibm commented Oct 20, 2026 •

Copy link
Copy Markdown

DynamicConfigWatcher.reconfigure_routing_logic rebuilds the router on every reload, but DynamicRouterConfig has no field for kv_aware_threshold, loadaware_beta, prefix_min_match_length or the five lmcache_* settings. The rebuilt router falls back to None or a default for them:

  • kvaware / loadaware: the LMCache controller is set up on 0.0.0.0:None, with None for the health check interval and worker timeout. If the controller could be built, the kvaware threshold would be None, and route_request would raise TypeError (len(token_ids) - None) on the first KV lookup hit. The loadaware beta falls back to 1.0.
  • prefixaware: prefix_min_match_length falls back to 0.

The file can't set these fields either: DynamicRouterConfig rejects them with TypeError: unexpected keyword argument, so every reload of such a file fails.

Fixing the fields isn't enough on its own. A reload closes the running router first, but KvawareRouter.close() only cancels the monitor task. The controller sockets stay bound, because lmcache 0.3.11's LMCacheControllerManager has no close(). So the rebuilt kvaware or loadaware router fails with ZMQError: Address already in use on the same port, and the watcher logs a warning and retries on every poll.

This PR:

  1. Adds the eight fields with the same defaults as the CLI flags, carries them in from_args, and passes them to reconfigure_routing_logic.
  2. Makes KvawareRouter.close() close the controller sockets on the kv manager loop, using LMCacheControllerManager.close() where lmcache provides it (newer versions do). It then waits up to one second until each controller port can be bound again, because closing a ZMQ socket returns before libzmq releases the port.

FIX #1141

Scope

The P/D labels (#1129) and max_instance_failover_reroute_attempts (#984) are left to those PRs, so this one doesn't overlap them. The issue also suggests one helper that builds the routing kwargs for both app.py and the watcher. That can follow once those two land. Doing it here would touch the same lines.

Not changed here:

  • A field the file leaves out gets the CLI default, the same as the existing fields. A value set only on the command line therefore doesn't survive a reload. This can happen at startup with no file edit, because the watcher's first poll reloads when the file leaves out fields such as k8s_port. A kvaware router started with --kv-aware-threshold 500 then runs with 2000, where on main the same reload removes the router. Keeping the value would change how every field reloads.
  • lmcache 0.3.11 runs its health check on its own thread with no way to stop it, so the old controller's thread stays alive after a reload. close() also leaves the router's own kv manager loop running, so each reload of a kvaware or loadaware router leaves one more idle thread behind.

Changes

  • src/vllm_router/dynamic_config.py: add the eight fields to DynamicRouterConfig, carry them in from_args, and pass them in reconfigure_routing_logic.
  • src/vllm_router/routers/routing_logic.py: close the LMCache controller sockets in KvawareRouter.close() and wait until the controller ports can be bound again.
  • src/tests/test_kvaware_router.py: start a kvaware router whose controller binds real ZMQ sockets the way lmcache 0.3.11 does, then reload it on the same ports through DynamicConfigWatcher.reconfigure_routing_logic. The test checks the threshold and the arguments LMCacheControllerManager receives.

Testing

  • On main, the new test fails with TypeError: ... unexpected keyword argument 'kv_aware_threshold'. With only the config change, it fails with ZMQError: Address already in use. With both changes it passes.
  • With the same fake controller, these reloads on one port succeed: loadaware → loadaware → loadaware, and roundrobin → kvaware → kvaware → loadaware. loadaware_beta is kept each time.
  • DynamicRouterConfig.from_yaml and from_json read the new fields from a file. A prefixaware reload keeps prefix_min_match_length.
  • pytest src/tests --ignore=src/tests/perftest: 284 passed.
  • pytest src/tests/test_kvaware_router.py -k reload --count 500: 500 passed, run twice.
  • pre-commit run --files <changed files>: pass.

  • Make sure the code changes pass the pre-commit checks.
  • Sign-off your commit by using -s when doing git commit
  • Try to classify PRs for easy understanding of the type of changes, such as [Bugfix], [Feat], and [CI].

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces several new configuration fields to DynamicRouterConfig for KV-cache-aware routing and LMcache controller settings, updating the configuration parsing and logic reconfiguration to support them. Additionally, a unit test is added to verify that dynamic configuration reloads preserve these KV-cache-aware settings. The review feedback suggests improving static type checking by changing type annotations from Optional[int] to int for fields with non-None defaults, and using getattr with safe defaults when parsing arguments to ensure backward compatibility and robustness against incomplete argument objects.

Comment on lines +75 to +82
kv_aware_threshold: Optional[int] = 2000
loadaware_beta: Optional[float] = None
prefix_min_match_length: Optional[int] = 0
lmcache_controller_port: Optional[int] = 9000
lmcache_controller_reply_port: Optional[int] = None
lmcache_controller_heartbeat_port: Optional[int] = None
lmcache_health_check_interval: Optional[int] = 5
lmcache_worker_timeout: Optional[int] = 30

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The fields kv_aware_threshold, prefix_min_match_length, lmcache_controller_port, lmcache_health_check_interval, and lmcache_worker_timeout are required to be non-None integers for the underlying routers and managers to function correctly. Changing their type annotations from Optional[int] to int clearly communicates that these fields cannot be None and helps with static type checking.

Suggested change
kv_aware_threshold: Optional[int] = 2000
loadaware_beta: Optional[float] = None
prefix_min_match_length: Optional[int] = 0
lmcache_controller_port: Optional[int] = 9000
lmcache_controller_reply_port: Optional[int] = None
lmcache_controller_heartbeat_port: Optional[int] = None
lmcache_health_check_interval: Optional[int] = 5
lmcache_worker_timeout: Optional[int] = 30
kv_aware_threshold: int = 2000
loadaware_beta: Optional[float] = None
prefix_min_match_length: int = 0
lmcache_controller_port: int = 9000
lmcache_controller_reply_port: Optional[int] = None
lmcache_controller_heartbeat_port: Optional[int] = None
lmcache_health_check_interval: int = 5
lmcache_worker_timeout: int = 30

Comment on lines +122 to +129
kv_aware_threshold=args.kv_aware_threshold,
loadaware_beta=args.loadaware_beta,
prefix_min_match_length=args.prefix_min_match_length,
lmcache_controller_port=args.lmcache_controller_port,
lmcache_controller_reply_port=args.lmcache_controller_reply_port,
lmcache_controller_heartbeat_port=args.lmcache_controller_heartbeat_port,
lmcache_health_check_interval=args.lmcache_health_check_interval,
lmcache_worker_timeout=args.lmcache_worker_timeout,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure backward compatibility and robustness against incomplete or mock args objects (e.g., in unit tests or programmatic invocations where some of these new attributes might be missing), we should use getattr with safe defaults instead of direct attribute access.

Suggested change
kv_aware_threshold=args.kv_aware_threshold,
loadaware_beta=args.loadaware_beta,
prefix_min_match_length=args.prefix_min_match_length,
lmcache_controller_port=args.lmcache_controller_port,
lmcache_controller_reply_port=args.lmcache_controller_reply_port,
lmcache_controller_heartbeat_port=args.lmcache_controller_heartbeat_port,
lmcache_health_check_interval=args.lmcache_health_check_interval,
lmcache_worker_timeout=args.lmcache_worker_timeout,
kv_aware_threshold=getattr(args, "kv_aware_threshold", 2000),
loadaware_beta=getattr(args, "loadaware_beta", None),
prefix_min_match_length=getattr(args, "prefix_min_match_length", 0),
lmcache_controller_port=getattr(args, "lmcache_controller_port", 9000),
lmcache_controller_reply_port=getattr(args, "lmcache_controller_reply_port", None),
lmcache_controller_heartbeat_port=getattr(args, "lmcache_controller_heartbeat_port", None),
lmcache_health_check_interval=getattr(args, "lmcache_health_check_interval", 5),
lmcache_worker_timeout=getattr(args, "lmcache_worker_timeout", 30),

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rahulssv-ibm the eight fields and the socket close are right, and the close is a leak I missed in #1141. One thing holds it back: the new test flakes.

blocking: close(linger=0) returns before libzmq releases the port, so the rebind in test_dynamic_config_reload_keeps_kvaware_settings races its I/O thread. Waiting until the pull port is bindable again inside close() took it to 0 of 60. A real reload hits the same race, though the watcher retries on the next poll.

non-blocking: the TypeError from #1141 is gone. A value set only on the command line still falls back to the default on reload (500 back to 2000 in my repro), which matches how every existing field already reloads. Keeping it would change that for all fields, so it is a separate call.

# head ddb2ffc, base 7b1164d, Python 3.12.3, pyzmq 27.2.0, vllm_router.__file__ read from each export
$ pytest -q src/tests --ignore=src/tests/perftest
273 passed, 1 warning                                    # head (272 at base)
$ for i in $(seq 60); do pytest -q src/tests/test_kvaware_router.py -k reload; done
EADDRINUSE 5/60                                          # head
EADDRINUSE 0/60                                          # head, close() polls a bind on the pull port for up to 1s
# dynamic_config.py from base, tests kept: TypeError: ... unexpected keyword argument 'kv_aware_threshold'
# routing_logic.py from base, tests kept:  zmq.error.ZMQError: Address already in use

$ python cli_only.py   # stub LMCacheControllerManager; startup kvaware with threshold=500, health=7; reload a file
startup  threshold=500 pull=0.0.0.0:9000 health=7
reload file keys=[] threshold=2000 pull=0.0.0.0:9000 health=5                        # head
reload file keys=['kv_aware_threshold', 'lmcache_health_check_interval'] threshold=500 pull=0.0.0.0:9000 health=7
reload file keys=[] threshold=None pull=0.0.0.0:None health=None                     # base
# not run: a real lmcache controller, pre-commit

Once this lands I'll open the helper that keeps startup values for fields a file leaves out as its own PR, so @ruizhang0101 can decide whether reload should work that way. Could you push the test fix first?

@rahulssv-ibm
rahulssv-ibm force-pushed the fix/dynamic-config-routing-args branch from ddb2ffc to e95c5e9 Compare October 23, 2026 01:38
@rahulssv-ibm

Copy link
Copy Markdown
Author

@chrikrah Pushed the test fix. After closing the controller sockets, KvawareRouter.close() now waits up to 1s in total until each controller port can be bound again. The probe sets SO_REUSEADDR the way libzmq does, so worker connections left in TIME_WAIT don't count. I also rebased onto main to resolve the conflict with #1129.

# head e95c5e9, Python 3.12, pyzmq 27.2.0
$ for i in $(seq N); do pytest -q src/tests/test_kvaware_router.py -k reload; done
EADDRINUSE 2/60                                      # before
EADDRINUSE 0/120                                     # after
$ pytest -q src/tests/test_kvaware_router.py -k reload --count 500   # one process, pytest-repeat
1 failed, 499 passed                                 # before
500 passed                                           # after
$ pytest -q src/tests --ignore=src/tests/perftest
275 passed

Agreed on leaving the CLI-only fallback to your follow-up PR.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rahulssv-ibm this replaces my request for changes from 10-22. The port wait in e95c5e9 fixes the flake I asked about, and nothing else blocks. From my perspective this can be merged.

# git archive e95c5e9, Python 3.12.3, pyzmq 27.2.0, pytest-repeat 0.9.4, vllm_router imported from the export
$ pytest -q src/tests/test_kvaware_router.py -k reload --count 500   # run twice
500 passed, 3000 deselected in 5.72s
500 passed, 3000 deselected in 5.98s
# same tree, routing_logic.py swapped for 10c5bff (before the wait), --count 300, run twice
10 failed, 290 passed, 1800 deselected in 4.21s
9 failed, 291 passed, 1800 deselected in 3.88s
$ pytest -q src/tests --ignore=src/tests/perftest
275 passed, 1 warning in 15.61s
# not run: a real lmcache controller, pre-commit

non-blocking: close() can now block for up to a second when a port stays taken. It then gives up without a log line. A debug message on timeout would tell an operator why the next bind still failed.

I will open the follow-up that keeps CLI-only values across a reload after this merges.

A dynamic config reload rebuilds the router from DynamicRouterConfig,
which had no field for kv_aware_threshold, loadaware_beta,
prefix_min_match_length or the five lmcache_* settings. The rebuilt
router fell back to None or a default for them. For kvaware the
threshold became None, so route_request raised a TypeError on the first
KV lookup hit, and the LMCache controller was set up on 0.0.0.0:None.
The file could not set these fields either, because DynamicRouterConfig
rejected the unknown keyword.

Add the eight fields with the CLI defaults, carry them in from_args and
pass them to reconfigure_routing_logic.

Signed-off-by: Rahul <[email protected]>
…router closes

A dynamic config reload closes the running router and builds a new one.
KvawareRouter.close() only cancelled the monitor task. The controller
sockets stayed bound, because lmcache 0.3.11's LMCacheControllerManager
has no close(). The new router then failed to bind the same port with
"Address already in use", so a kvaware or loadaware router could not be
reloaded.

Close the controller sockets on the kv manager loop, using
LMCacheControllerManager.close() where lmcache provides it. The reload
test now starts a kvaware router on real ZMQ sockets and reloads it on
the same ports.

Signed-off-by: Rahul <[email protected]>
… on close

Closing a ZMQ socket returns before libzmq's I/O thread unbinds the
port. A router rebuilt right after KvawareRouter.close() could still hit
"Address already in use", which made the dynamic config reload test
flaky. A real reload recovered only on the next poll.

After closing the controller sockets, wait up to one second until each
controller port can be bound again.

Signed-off-by: Rahul <[email protected]>
@rahulssv-ibm
rahulssv-ibm force-pushed the fix/dynamic-config-routing-args branch from e95c5e9 to e6162a2 Compare November 3, 2026 10:16
@rahulssv-ibm

Copy link
Copy Markdown
Author

k8s-discovery-e2e-test failed here on RuntimeError: cudaHostAlloc failed: 1 during engine startup in the roundrobin deploy. It isn't from this PR: main fails the same way (run 41551494154), roundrobin failed in 13 of the last 60 jobs, and the other four tests passed in this run.

I opened #1196 so the job can recover from it. In the meantime, @ruizhang0101 could you re-run the failed job here? I don't have access to re-run it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: a dynamic config reload drops the router's tuning arguments, and kvaware ends up with threshold None

2 participants