Skip to content

[Code scan] Close TensorFlow sessions in C++ API destructors #5657

Description

@njzjz

This issue comes from a Codex global scan of deepmodeling/deepmd-kit at commit 73de44b1f94471b2e3bdb6b11f57b34d7bc791bb.

Problem

Several TensorFlow C++ API wrappers create a tensorflow::Session with NewSession, but their destructors only delete the GraphDef.

The same lifetime pattern appears in the other TensorFlow API wrappers:

The constructor catch blocks also only delete graph_def if init() throws, so failures after NewSession can leak the partially constructed session before the destructor can run.

Impact

Repeated construction/destruction of TensorFlow C++ API objects can leak TensorFlow sessions, including associated CPU/GPU resources and runtime threads. This is especially relevant for long-running host applications, plugins, or test processes that load many models.

Suggested fix

Close and delete the TensorFlow session in each destructor, and clean it up on constructor failure after NewSession succeeds. Prefer RAII ownership for both session and graph_def so partial initialization and exceptions follow the same cleanup path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions