Skip to content

Preserve ownership of borrowed COM pointer views - #1012

Open
Robolightning wants to merge 1 commit into
enthought:mainfrom
StableKite:fix/borrowed-com-pointer-ownership
Open

Robolightning wants to merge 1 commit into
enthought:mainfrom
StableKite:fix/borrowed-com-pointer-ownership

Conversation

@Robolightning

Copy link
Copy Markdown
Contributor

Problem

Reading a COM pointer through ctypes (for example through .contents or a structure field) creates a view of existing storage without taking a COM reference. The view currently calls Release() when it is collected. With an in-process test object and an extra reference for safe cleanup, collecting either kind of view lowers the reference count from two to one on the current main branch.

Change

Skip the destructor's Release() for pointer views with a ctypes base. In the VT_RECORD and SAFEARRAY paths that explicitly take a temporary reference, give it an independent pointer wrapper so that the matching Release() still occurs. The tests also check that the returned VARIANT and SAFEARRAY pointers continue to own their own references.

The ctypes documentation describes how a view keeps its underlying ctypes storage alive. The COM reference counting rules cover borrowing a reference whose owner outlives the view.

Verification

  • Windows x64, CPython 3.15.2 free-threaded with the GIL disabled. The new ownership test fails for both view types on main and passes with this change.
  • On 3.15.2t, 39 borrowed-pointer, VARIANT and SAFEARRAY tests passed, with one pre-existing skip.
  • The three new tests also passed on CPython 3.12.
  • The tests that require the registered out-of-process COM test server were skipped locally.

@codecov-commenter

codecov-commenter commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.94%. Comparing base (232e8eb) to head (fe5fec8).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1012      +/-   ##
==========================================
+ Coverage   92.92%   92.94%   +0.02%     
==========================================
  Files         153      154       +1     
  Lines       16588    16647      +59     
==========================================
+ Hits        15414    15473      +59     
  Misses       1174     1174              

☔ 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 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for your contributions.

I have also been concerned about the ownership and lifetime of interface pointers assigned to record fields, so I first added #1014 and #1015 as characterization tests.

Since this PR will likely affect the test below, we should probably merge main branch into this branch to verify.

def test_reading_fields_then_del_releases_sentinel(self):
obj = _create_server()
rec = obj.GetRecord(4)
self.assertEqual(rec.tag, 1)
self.assertEqual(rec.text, "aaaa")
unk = rec.unk
self.assertTrue(unk)
# Reading fields returns values without transferring ownership, so the
# sentinel is still alive.
self.assertEqual(obj.GetLiveSentinelCount(), 1)
del unk
self.assertEqual(
obj.GetLiveSentinelCount(),
0,
msg="CHARACTERIZATION: `unk` is a borrowed pointer, so it should "
"NOT be released here. The record still owns the sentinel, so the "
"live count should be 1.",
)
del rec
gc.collect()
self.assertEqual(
obj.GetLiveSentinelCount(),
0,
msg="CHARACTERIZATION: This is correct if the record is really "
"released and the sentinel is also released. But this count ONLY "
"reflects the release of `unk` above. Once the borrowed-pointer "
"release is fixed, this live count will be 1 until the feature "
"that automatically releases a record returned from a COM vtable "
"method is implemented. See #1014 and #1012.",
)

Before proceeding with the discussion, allow me to note a few points regarding the basic test structure.

Comment on lines +26 to +29
if isinstance(holder, PointerField):
view = holder.pointer
else:
view = holder.contents

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should write two separate tests instead of using if.

Comment on lines +36 to +37
if remaining == 2:
pointer.Release()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This hides what the test is really checking.
Or does this mean the reference count is accidentally incrementing with no reproducibility and for an unknown reason?

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.

3 participants