Commit 75863c5
committed
fix: Do not load the native extension to build an empty rules list
Session.__init__ calls _build_expr_host_rules() unconditionally, and that
imported openjd.expr -- a facade over the native openjd._openjd_rs
extension -- before checking whether there was anything to convert. With no
path mapping rules it imported the engine, iterated an empty list, and
returned []. So a worker that never evaluates an EXPR expression still
loaded the extension on its FIRST SESSION: 49d1804 moved the load from
import time to first-session time rather than eliminating it. Reported by
a reviewer reading the call site; confirmed by measurement.
Return [] directly when there are no rules. That is byte-identical to what
the loop produced, and it must stay [] rather than None because the session
is still host scope and apply_path_mapping() has to remain available with an
empty rule set. Seeding [] is free: SymbolTable stores expr_host_rules
untouched as Optional[list[Any]] and only crosses into Rust at evaluation
time, in _expr_support.symtab_to_expr_values -- verified that assigning
None, [], and running a full non-EXPR resolve all leave the extension
unloaded.
Audited every load path rather than grepping for imports, using a
sys.meta_path finder that records the stack at the moment
openjd._openjd_rs is first imported, one flow per fresh interpreter.
Before: four flows leaked -- Session() with no rules, with rules=None, a
non-EXPR run_task, and a non-EXPR enter_environment -- and all four traced
to the same single frame, _session.py:476 __init__ ->
_build_expr_host_rules. After: every non-EXPR flow is clean, and the only
remaining loads are the three that should load. Import of openjd.sessions,
non-EXPR argument resolution, and non-EXPR optional-int resolution were
already clean and stayed clean.
Known limitation, asserted rather than left implicit: a session with real
path mapping rules still loads the extension, because the rules must be
converted into openjd.expr.PathMappingRule engine objects to seed host
context. openjd-sessions cannot avoid that alone; closing it needs
openjd-model to accept unconverted rules and convert them at the Rust
boundary it already crosses. test_path_mapping_rules_do_load_the_extension
pins the current behaviour so that a future fix is noticed here instead of
passing silently. This matters in practice: a worker with path mapping
rules configured -- the common case -- still loads the extension.
Correcting my own earlier audit: I classified this site as "already
function-local (lazy)" and moved on. Lazy is not conditional. A
function-local import still runs unconditionally the moment its function is
called, and this one is called from __init__. The existing purity tests
covered import time plus one runtime path, so nothing would have caught it.
Mutation-checked, 4 mutants, 0 survivors: neutralising the fast path,
returning None instead of [], firing it for every session, and inverting
its condition are each caught by name.
Verified: 913 passed / 39 skipped / 16 xfailed; ruff, black, mypy native
and mypy --platform win32 all clean.
Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>1 parent e2e60d3 commit 75863c5
2 files changed
Lines changed: 143 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1653 | 1653 | | |
1654 | 1654 | | |
1655 | 1655 | | |
| 1656 | + | |
| 1657 | + | |
| 1658 | + | |
| 1659 | + | |
| 1660 | + | |
| 1661 | + | |
| 1662 | + | |
| 1663 | + | |
| 1664 | + | |
| 1665 | + | |
| 1666 | + | |
| 1667 | + | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
1656 | 1673 | | |
1657 | 1674 | | |
1658 | 1675 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 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 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
0 commit comments