Commit 4a0b95d
fix(tf): close C++ API sessions (#5800)
Closes #5657.
## Summary
- initialize the TensorFlow `Session*` member to `nullptr` in
`DeepPotTF`, `DeepSpinTF`, `DeepTensorTF`, and `DipoleChargeModifierTF`;
- centralize exclusive session teardown as `Close()` followed by
`delete` and pointer reset;
- use an initialization rollback guard so public `init()` calls are
failure-atomic and can be retried without overwriting a session left by
an earlier failed attempt;
- clean sessions from both initializing-constructor exception paths and
normal destructors;
- make `DeepPotJAX` initialization failure-atomic by unconditionally
releasing all partially created TensorFlow C API handles (`TF_Session`,
`TF_Graph`, `TF_SessionOptions`, `TF_Status`, `TFE_Context`,
`TFE_ContextOptions`, and `TF_Function` references);
- add direct lifecycle regressions for failed initialization/retry and
successful initialization/normal destruction.
## Root cause
The TensorFlow wrappers owned raw sessions created by `NewSession`, but
their destructors and initializing-constructor catch blocks only deleted
the `GraphDef`. Default construction followed by a failing public
`init()` could also retain a partially initialized session, allowing a
retry to overwrite the pointer.
`DeepPotJAX` had the same failure mode with TensorFlow C API handles:
its destructor only released resources when `inited == true`, so an
exception before the final initialization commit leaked every handle
created up to that point.
The TensorFlow rollback guard now owns cleanup until initialization has
completely committed. `DeepPotJAX::clear_tf_resources()` is null-safe
for partial initialization and is called both from the exception path
and the destructor. Successful initialization commits only after all
metadata has been loaded.
## Test coverage
The lifecycle test intentionally white-box-links `deepmd_backend_tf` and
`deepmd_backend_jax` into `runUnitTests_cc`, rather than going through
the `dlopen` plugin facade. This makes the concrete backend destructors
directly visible to the LSan test process.
The tests cover:
- repeated failed initialization followed by safe destruction for all
four TensorFlow C++ wrappers;
- successful initialization followed by normal destruction for all four
TensorFlow C++ wrappers;
- repeated failed `DeepPotJAX` initialization;
- successful `DeepPotJAX` initialization followed by normal destruction
when the generated SavedModel test artifact is available.
Ordinary builds verify retry safety and destructor behavior; the CI
leak-sanitizer leg provides the direct leak regression signal.
## Validation
- built `deepmd_backend_tf`, `deepmd_backend_jax`, `runUnitTests_cc`,
and `deepmd_op` in a TensorFlow/JAX C++ test configuration with LAMMPS
plugin mode disabled;
- `TestTensorFlowSessionLifecycle.*`: 3 passed, 1 skipped locally
because the generated JAX SavedModel artifact was unavailable;
- `ruff check .`;
- `ruff format .`;
- clang-format check;
- CMake formatting hook;
- `git diff --check`.
Coding agent: Codex
Codex version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning effort: xhigh
---------
Co-authored-by: njzjz-bot <njzjz-bot@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: njzjz-bot <njzjz.bot@gmail.com>1 parent 91b722c commit 4a0b95d
9 files changed
Lines changed: 409 additions & 152 deletions
File tree
- source/api_cc
- include
- src
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
242 | 242 | | |
243 | 243 | | |
244 | 244 | | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
245 | 252 | | |
246 | 253 | | |
247 | 254 | | |
| |||
284 | 291 | | |
285 | 292 | | |
286 | 293 | | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
293 | 300 | | |
294 | 301 | | |
295 | 302 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 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 | + | |
21 | 66 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| 24 | + | |
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
24 | 28 | | |
25 | 29 | | |
26 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
27 | 34 | | |
28 | 35 | | |
29 | 36 | | |
| |||
34 | 41 | | |
35 | 42 | | |
36 | 43 | | |
| 44 | + | |
37 | 45 | | |
38 | 46 | | |
39 | 47 | | |
| |||
72 | 80 | | |
73 | 81 | | |
74 | 82 | | |
| 83 | + | |
75 | 84 | | |
76 | 85 | | |
77 | 86 | | |
| |||
0 commit comments