Skip to content

fix: close ViewStore on IModelDb.close and disconnect V2 checkpoint containers on shutdown - #9539

Merged
anmolshres98 merged 14 commits into
masterfrom
anmolshres98/issue-5017-flaky-handle-leak
Aug 5, 2026
Merged

fix: close ViewStore on IModelDb.close and disconnect V2 checkpoint containers on shutdown#9539
anmolshres98 merged 14 commits into
masterfrom
anmolshres98/issue-5017-flaky-handle-leak

Conversation

@anmolshres98

@anmolshres98 anmolshres98 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Addresses #5017 (the residual handle leaks Travis described in the May 2025 comments, not the original certa bug fixed by #8020).

Leakage

  • IModelDb.close() never closed the ViewStore created by accessViewStore(), leaving its CloudContainer connected with an hourly token-refresh timer
  • V2 checkpoint containers were only disconnected by the test-only V2CheckpointManager.cleanup(), never on host shutdown

-- created w/ help from AnmolDroid🤖

…ontainers on shutdown so cloud containers no longer leak (#5017)
Copilot AI review requested due to automatic review settings July 23, 2026 21:20

Copilot AI 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.

Pull request overview

This PR addresses residual cloud container handle leaks by ensuring CloudSqlite-backed resources are deterministically disconnected during iModel close and host shutdown, and by making container leaks visible again by allowing connected containers to keep the Node process alive.

Changes:

  • Close an iModel’s ViewStore during IModelDb.close() and clarify ViewStore ownership semantics.
  • Disconnect V2 checkpoint containers during IModelHost.shutdown() via V2CheckpointManager.cleanup().
  • Revert CloudSqlite token-refresh timer .unref() behavior so connected containers keep the process alive; tighten mocha-reporter leak detection timeout; add test hygiene + docs/change notes.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tools/build/src/mocha-reporter/index.ts Reduce default handle-leak detection timeout and simplify call site.
full-stack-tests/backend/src/integration/CloudWorkspace.test.ts Ensure OwnedWorkspace is closed to prevent leaked containers during tests.
docs/changehistory/NextVersion.md Document cloud container lifecycle behavior changes and leak-failure behavior.
core/backend/src/IModelHost.ts Invoke V2 checkpoint cleanup during shutdown (note: currently adds a static import).
core/backend/src/IModelDb.ts Close ViewStore when iModel closes; document that the iModel owns its ViewStore.
core/backend/src/CloudSqlite.ts Remove .unref() from token-refresh timer to keep process alive when containers leak.
core/backend/src/CheckpointManager.ts Update cleanup documentation to reflect shutdown usage.
common/changes/@itwin/core-backend/*.json Rush change note for backend lifecycle/leak behavior.
common/changes/@itwin/build-tools/*.json Rush change note for mocha-reporter timeout reduction.
common/api/core-backend.api.md API report update for new internal close hook + doc tag adjustments.
Comments suppressed due to low confidence (1)

core/backend/src/IModelHost.ts:722

  • Calling V2CheckpointManager.cleanup() via a static import makes IModelHost depend on CheckpointManager at module load time, creating a circular dependency (CheckpointManager imports IModelHost). Consider dynamically importing CheckpointManager here to avoid circular-load edge cases during startup/shutdown.
    // safe to disconnect checkpoint containers here: open iModels were already closed by IModelDb's onBeforeShutdown listener above
    V2CheckpointManager.cleanup();
    CloudSqlite.CloudCaches.destroy();

Comment thread core/backend/src/IModelHost.ts
@anmolshres98

Copy link
Copy Markdown
Contributor Author

/azp run iTwin.js, iTwin.js Integration - GitHub, iTwin.js Docs - YAML

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@iTwin iTwin deleted a comment from azure-pipelines Bot Jul 23, 2026
@iTwin iTwin deleted a comment from azure-pipelines Bot Jul 23, 2026
@anmolshres98 anmolshres98 changed the title [WIP] fix: close ViewStore on IModelDb.close and disconnect V2 checkpoint containers on shutdown so cloud containers no longer leak (#5017) [WIP] fix: close ViewStore on IModelDb.close and disconnect V2 checkpoint containers on shutdown Jul 24, 2026
@anmolshres98 anmolshres98 linked an issue Jul 24, 2026 that may be closed by this pull request
Comment thread docs/changehistory/NextVersion.md Outdated
@anmolshres98 anmolshres98 changed the title [WIP] fix: close ViewStore on IModelDb.close and disconnect V2 checkpoint containers on shutdown fix: close ViewStore on IModelDb.close and disconnect V2 checkpoint containers on shutdown Aug 4, 2026
@anmolshres98
anmolshres98 marked this pull request as ready for review August 4, 2026 14:19
@anmolshres98
anmolshres98 requested review from a team and wgoehrig as code owners August 4, 2026 14:19

@aruniverse aruniverse left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Add deterministic coverage for IModelDb.close() closing its ViewStore and IModelHost.shutdown() cleaning a populated V2 checkpoint manager. The modified CloudWorkspace test is correct hygiene, but it does not exercise either production lifecycle fix. I did not duplicate the existing unresolved circular-import thread.

Comment thread core/backend/src/CloudSqlite.ts
Comment thread tools/build/src/mocha-reporter/index.ts
Comment thread core/backend/src/IModelDb.ts
@anmolshres98

Copy link
Copy Markdown
Contributor Author

please restore a focused entry explaining both automatic host-owned cleanup and the need to disconnect caller-owned containers

as per discussion in this #9539 (comment), didn't add anything to NextVersion.md in my recent commits. I think this was your copilot's take? Unless you changed your mind @aruniverse ?

@aruniverse

Copy link
Copy Markdown
Member

please restore a focused entry explaining both automatic host-owned cleanup and the need to disconnect caller-owned containers

as per discussion in this (earlier comment), didn't add anything to NextVersion.md in my recent commits. I think this was your copilot's take? Unless you changed your mind @aruniverse ?

yes ignore this ai take

@aruniverse aruniverse left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Follow-up review of the latest fixes found two new issues; existing unresolved feedback was not duplicated.

Comment thread core/backend/src/CloudSqlite.ts Outdated
…nmolshres98/issue-5017-flaky-handle-leak

@aruniverse aruniverse left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The latest production fixes address the prior stale-refresh and change-note comments. One new test-coverage/cleanup issue remains.

Comment thread core/backend/src/test/standalone/CloudSqlite.test.ts Outdated
@mergify

mergify Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@anmolshres98
anmolshres98 merged commit bf8e50e into master Aug 5, 2026
21 checks passed
@anmolshres98
anmolshres98 deleted the anmolshres98/issue-5017-flaky-handle-leak branch August 5, 2026 17:08
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.

Flaky test - Handle leak detected

3 participants