Skip to content

Choose the right find_memleak target for each server test. - #1007

Merged
junkmd merged 12 commits into
enthought:mainfrom
junkmd:memleak_target
Nov 22, 2026
Merged

junkmd merged 12 commits into
enthought:mainfrom
junkmd:memleak_target

Conversation

@junkmd

@junkmd junkmd commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Related to #971

Summary

Now each server test picks the find_memleak target that fits its server. So the leak check uses the right method for each case.

Why

Before this change, every server test used the same target: "working_set".

The "working_set" target measures the amount of memory pages in RAM right now. It is noisy.
It can go up even when there is no leak. It can stay flat even when there is a leak.
So the test sometimes failed in CI for no real reason, and sometimes missed a real leak. This is the problem in #971.

There is a second problem. A local server runs in a different process.
"mallocspy" only sees memory in the current process. So "mallocspy" cannot see the server's memory in a local server test.
We must measure this process's committed memory instead.

So one target cannot fit all tests. Each test needs the right tool.
This PR tries to remove false positives and false negatives by switching the method to fit the server and the method under test.

This PR is the next step after #996, #997, and #1002. #996 added "private_usage". #997 added the IMallocSpy interface. #1002 added the "mallocspy" target. This PR uses those targets in the server tests.

What the tests check

  • TestInproc and TestInproc_win32com use "mallocspy", because the server runs in this process.
  • TestLocalServer and TestLocalServer_win32com use "private_usage", because the server runs in another process.
  • _find_memleak still fails the test when the target reports a leak.

These tests are important because they prove each test uses the right method.
If a test uses the wrong method, it can fail for no reason, or it can miss a real leak.

Comments in the code

The place where each test sets its target has a comment. The comment says why that target is right for that test.
For example, the in-process tests say that "mallocspy" can count the server's COM allocations exactly.
The local server tests say that "mallocspy" cannot see the other process, so we measure this process's committed memory instead.

These comments help the next reader. A reader can see the reason without reading the whole issue.

Trade-offs

Pros:

  • In-process tests are exact and stable, because "mallocspy" counts COM allocations.
  • Local server tests measure committed memory ("private_usage"), which is better than "working_set".
  • Each test can pick the best method for its own case.

Cons:

  • Each test class must set _memleak_target by hand. If a new test class forgets it, the test will fail with an error.
  • "private_usage" is still noisy, because it measures the whole process.

Justification

The change does not change the old behavior of find_memleak. It only chooses a target per test. The old "working_set" target still works and is still the default.

We do not make "mallocspy" the only way. We keep "working_set" and "private_usage" too, so each test can pick the best method.

Limits

This change does not make the tests fully stable. A better method may be found later. But this is the best we can do now.
It should have fewer false positives and false negatives than "working_set".

Out of Scope

  • Changing the default target of find_memleak.
  • Changing the old "working_set" and "private_usage" behavior.
  • Removing the "working_set" target.

`BSTR` values are made by `SysAllocString`, and this goes through the COM task
allocator. So `mallocspy` can count them exactly.
The old `working_set` target gave a false leak, because the first call in a
process brings in pages that look like a leak.
This change makes `check_leaks` use `find_memleak(func, target="mallocspy")`.
Then `test_creation` does not need the old `limit=4096` workaround, so we
remove it.
`VARIANT(MYCOLOR(...))` stores the record as a `VT_RECORD`, and its data is
allocated with the COM task allocator. So `mallocspy` can count this
allocation exactly.
The old `working_set` target gave a false leak, because the first call in a
process brings in pages that look like a leak.
This change makes `VariantTest` use `find_memleak(func, target="mallocspy")`.
`from_param` creates a `SAFEARRAY` of `MYCOLOR` records. `mallocspy` counts
the `SAFEARRAY` descriptor and data exactly.
The old `working_set` target gave a false leak, because the first call in a
process brings in pages that look like a leak.
This change makes `SafeArrayTest` use `find_memleak(doit, target="mallocspy")`.
This test was skipped with "This fails with a memory leak. Figure out if
false positive." The leak was a false positive from `working_set`, which
measures resident pages and reports first-call page faults as leaks.
`mallocspy` counts the COM task allocations of the `VARIANT`'s `SAFEARRAY`
exactly, so the test can run again.
This test was skipped with "This fails with a memory leak. Figure out if
false positive." The leak was a false positive from `working_set`, which
measures resident pages and reports first-call page faults as leaks.
`mallocspy` counts the COM task allocation of the `VARIANT`'s `SAFEARRAY` of
doubles exactly, so the test can run again.
This test was skipped with "This fails with a memory leak. Figure out if
false positive." The leak was a false positive from `working_set`, which
measures resident pages and reports first-call page faults as leaks.
`mallocspy` counts the `SAFEARRAY` of `BSTR`s and its elements exactly, so
the test can run again.
This test was skipped with "This fails with a memory leak. Figure out if
false positive." The leak was a false positive from `working_set`, which
measures resident pages and reports first-call page faults as leaks.
`mallocspy` counts the `SAFEARRAY` of longs exactly, so the test can run
again.
This test was skipped with "This test takes a long time. Do we need it? Can it
be rewritten?" The slowness came from `working_set`, which needs many
iterations to stabilize. `mallocspy` counts the COM task allocations of the
iterated `BSTR`s and `VARIANT`s exactly, so the test is deterministic and can
run again.
This test was skipped with "This test takes a long time. Do we need it? Can it
be rewritten?" The slowness came from `working_set`, which needs many
iterations to stabilize. `mallocspy` counts the COM task allocations of the
`VARIANT`s returned by `Next` exactly, so the test is deterministic and can
run again.
This test was skipped with "This test takes a long time. Do we need it? Can it
be rewritten?" The slowness came from `working_set`, which needs many
iterations to stabilize. `mallocspy` counts the COM task allocations of the
`BSTR`s and `VARIANT`s from iterating the collection twice exactly, so the
test is deterministic and can run again.
…rvers.

`BaseServerTest._find_memleak` now reads a `_memleak_target` class attribute
instead of always using `private_usage`. The right target depends on where the
server runs:
- In-process servers (`TestInproc`, `TestInproc_win32com`) use `mallocspy`,
  which counts their COM task allocations exactly.
- Out-of-process servers (`TestLocalServer`, `TestLocalServer_win32com`)
  use `private_usage`, because `mallocspy` cannot see allocations made
  in another process.

This keeps the exact counting where it is possible and falls back to commit
charge only where it is needed.
`TestInproc` overrode `test_eval` with a skip, because it "fails occasionally
with a memory leak on INPROC". The override body was only `pass`, so it
disabled the inherited `BaseServerTest.test_eval`.

In-process servers now use `mallocspy`, which counts COM task allocations
exactly, so the occasional false leak is gone. Removing the override lets
`test_eval` run again for the in-process server.
@junkmd junkmd added this to the 1.5.1 milestone Nov 22, 2026
@junkmd junkmd added the tests enhance or fix tests label Nov 22, 2026
@codecov-commenter

codecov-commenter commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.58%. Comparing base (aa3b444) to head (2c7ecdf).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1007      +/-   ##
==========================================
+ Coverage   92.29%   92.58%   +0.28%     
==========================================
  Files         150      150              
  Lines       16064    16059       -5     
==========================================
+ Hits        14827    14868      +41     
+ Misses       1237     1191      -46     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@junkmd
junkmd merged commit 61ccb1f into enthought:main Nov 22, 2026
134 of 135 checks passed
@junkmd
junkmd deleted the memleak_target branch November 22, 2026 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests enhance or fix tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants