Skip to content

Refresh args and __cause__ of a __namespace__ exception created from .NET - #174

Open
quantconnect-kronos[bot] wants to merge 5 commits into
masterfrom
kronos/bug-168-refresh-dotnet-exception-args
Open

quantconnect-kronos[bot] wants to merge 5 commits into
masterfrom
kronos/bug-168-refresh-dotnet-exception-args

Conversation

@quantconnect-kronos

@quantconnect-kronos quantconnect-kronos Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Closes #168

What changed and why

When .NET constructs a generated (__namespace__) exception subclass, for example through Activator.CreateInstance or ConstructorInfo.Invoke, PythonDerivedType.InvokeCtor creates its Python object before the base constructor runs. CLRObject.Create sets the exception's args and __cause__ at that point, from a Message and InnerException the base constructor hasn't set yet, so args held the default message and __cause__ stayed None.

InvokeCtor now sets args and __cause__ again once the base constructor has run, only on that .NET path (where it created the Python object itself). The registered Python object is kept, and the constructor runs once. Construction from Python is unchanged: ExceptionClassDerivedObject.Init already refreshes them there. Non-exception types are unchanged.

  • .NET construction doesn't run the Python __init__; a caller may run it afterwards (the two-step pattern of test_construction_from_clr). So the refresh reads Message as .NET implements it, without Python overrides, the same way CLRObject.Create read it: with the Python object unset for that read. args then holds the message passed to the constructor, and no Python code runs during construction.
  • The refresh holds a strong reference to the Python object, from before reading Message until args and __cause__ are set, so a collection during it leaves a valid object. It releases the GIL while .NET reads Message, so a getter that waits on another thread running Python code doesn't deadlock.
  • When it can't read Message (a .NET override that throws), or can't set args (a message Python can't hold, an args setter that rejects it), the exception keeps the args it was created with, and construction succeeds as on master. A message Python can't hold is reported without a managed exception. When it can't convert the inner exception (its Message throws, or Python can't hold it), construction succeeds without a __cause__.
  • CLRObject.Create frees the Python object it allocated when reading or converting the wrapped exception's Message throws, which kept that exception alive, and SetArgsAndCause checks the message before allocating the args tuple.
  • Exceptions.SetArgsAndCause converts a generated inner exception with ClassDerivedObject.ToPython, so __cause__ is the inner exception's own Python object (outer.__cause__ is inner) instead of a second wrapper. It does so only while that object's CLR wrapper is alive: when .NET has collected it (a Python-created exception held only by .NET, such as a faulted Task's), the inner exception gets a new wrapper as on master. This also applies to construction from Python, which had the same problem. An inner exception without a Python object in this runtime, such as one created without a constructor, gets a wrapper as on master. The other callers of SetArgsAndCause read Message as on master.

Known limits

As decided in #174 (comment), these stay as they are:

  • args holds the message the .NET constructor received, not the value of a Python get_Message override.
  • When args can't be set again, the exception keeps the args it was created with, and __cause__ isn't set (as on master).
  • When a .NET override of Message throws after construction, or needs the exception's Python object, the exception keeps the args it was created with.

As decided in #174 (comment), this is accepted too:

  • A generated exception constructed from .NET whose InnerException refers back to it, such as through inner.Data, stays alive with its inner exception, as an exception constructed from Python with the same back-reference already does on master: the outer Python object's __cause__ holds the inner one, which holds the outer.

As decided in #174 (comment), this pull request stays at e109105, apart from e734894: that fixes a regression in an exception constructed, raised, caught and read from Python, which the decision still treats as blocking. A new finding that needs a custom C# exception class, reflection beyond the plain constructor call, threads, forced garbage collection, or unusual Message or args behaviour goes here as a known limit instead of a change. Known limits under it:

  • When .NET has collected a generated inner exception's CLR wrapper before .NET constructs the outer exception, __cause__ gets a new wrapper, which stays alive after the outer exception is dropped: 20,000 such constructions keep about 4 MB, where master leaves __cause__ unset. An exception constructed from Python with such a cause keeps the same amount as on master.
  • A generated exception constructed from .NET whose inner exception is a custom CLR exception, with a Message that waits on Python code running on another thread, can stall while __cause__ is set.

Tests

test_namespaced_exception_subclass_created_from_dotnet (subprocess, like #167's tests) constructs __namespace__ subclasses of System.Exception from .NET and checks:

  • args and __cause__ match Message and InnerException for Activator.CreateInstance(type, [message, inner]), ConstructorInfo.Invoke([message]), the parameterless constructor, and a two-level __namespace__ chain.
  • Delayed initialization: a get_Message override that returns state set by a clrmethod __init__ constructs, and returns "initialized" once the caller invokes __init__.
  • Object validity: an instance whose get_Message runs GC.Collect() stays a valid object after construction and another collection.
  • A generated inner exception with a Python instance attribute is outer.__cause__ itself.

It fails on master (__cause__ is a different object, without the attribute) and on the previous head of this branch (DelayedError construction throws 'DelayedError' object has no attribute 'text').

A separate probe also covers the reviews' other cases, each as on master or better:

  • a get_Message that returns a string and then None: ('m',)
  • an args setter that rejects a second assignment: it keeps the first
  • a message with an unpaired UTF-16 surrogate built in .NET: constructs; 15,000 of them retain about as much memory as on master
  • a .NET base Message override that throws after construction, or passes the exception to a Python delegate: constructs, with the args it was created with, as on master
  • a .NET base Message override, or a Python args setter that keeps self, followed by collections: the object stays valid
  • new Exception("outer", <generated exception created without a constructor>): converts as on master
  • a Python-created generated exception whose get_Message counts its calls: ('2',), as on master
  • a .NET base Message that waits on a worker running a Python delegate: constructs in about 20 ms, with the worker's result in args
  • a CLR inner exception whose Message throws, or holds an unpaired surrogate: constructs, and the inner exception is collected, as on master
  • from Python, raise Error("workflow failed", failed.Exception) where failed = Task.FromException(Error("inner")), after ordinary allocations: error.__cause__.__cause__.Message == 'inner', as on master (e109105 raised TypeError: invalid target)

The CI steps on this head, Python 3.11 / .NET 10:

  • dotnet test src/embed_tests/ --filter "FullyQualifiedName!~SetPythonPath": 1047 passed, 8 skipped
  • dotnet test src/embed_tests/ --filter "FullyQualifiedName~SetPythonPath": 1 passed
  • pytest --runtime netcore tests: 493 passed, 23 skipped
  • dotnet test src/python_tests_runner/: 2 passed

Also tested end to end on e734894, with the CLRObject.Create change left out of that build (none of these cases reaches it). An algorithm passed every check:

  • message and inner exception, constructed from .NET: args == ('custom message',), with a cause whose Message is 'inner'
  • message only: args == ('message only',) with no cause; args is kept after raising and catching it in Python
  • delayed __init__: Message == 'initialized' with args == ('base message',)
  • get_Message running GC.Collect(): still a valid CollectingError
  • generated inner exception: outer.__cause__ is inner, keeping its attribute
  • an args setter keeping self, followed by a collection: still a valid object with ('saving',)
  • a Python-created exception whose get_Message counts its calls: ('2',)
  • from Python, raise Error("workflow failed", failed.Exception) with failed = Task.FromException(Error("inner")), after allocations and collections, then caught: args == ('workflow failed',) and __cause__.__cause__.Message == 'inner'
  • from Python, raise Error("outer", inner) with a live generated inner, then caught: __cause__ is inner, keeping its attribute

@quantconnect-kronos quantconnect-kronos Bot added kronos:develop:working develop: in progress kronos:review Request review labels Nov 23, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 23, 2026

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Requesting changes because of how the branch is stacked, not because of the fix itself. The fix (5f4ebf3) does what #168 asks, and I found nothing in it that blocks.

Blocking: this branch can't merge before #167. It contains #167's five commits, and #167 still has an open change request: a get_Message override error reached through an inherited ToString with two or more generated levels comes back to Python wrapped in a TargetInvocationException. #167 is also waiting for a person to decide how to continue. If this pull request were approved and merged now, it would bring in all of #167 with that finding still open. Once #167 merges, rebase this onto just the last commit and I'll review that.

The fix itself (5f4ebf3)

  • The refresh runs only when InvokeCtor created the Python object itself, and only after the base constructor has run. The registered wrapper stays, and the constructor runs once. With a two-level chain, the inner generated constructor finds __pyobj__ already set and skips the refresh, so only the outermost level refreshes.
  • From Python, nothing changes, because __pyobj__ is set before the constructor runs. That path still gets its refresh from ExceptionClassDerivedObject.Init.
  • A raising get_Message override now makes construction from .NET throw: Activator.CreateInstance raises a TargetInvocationException wrapping the Python error. Construction from Python already fails the same way, and on master this path aborted the process, so nothing gets worse.

What I verified (Python 3.11 / .NET 10)

  • All nine CI checks pass, including the Lean regression and unit test jobs.
  • tests/test_exceptions.py: 33 passed on this head. With only ClassDerived.cs reverted to #167's head, test_namespaced_exception_subclass_created_from_dotnet fails, with the stale default message in args and no __cause__.
  • Extra checks, all exiting normally:
    • with a get_Message override, args is ('overridden',)
    • a raising override reaches the caller as a catchable error
    • a .NET-created instance raised and caught in Python keeps its args and __cause__
    • 2,000 instances created from .NET, followed by .NET and Python garbage collection

I set risk:high because, as it stands, merging this pull request would ship #167's changes, which are risk:high and not yet approved. Once the branch is just the one commit, the label can be judged on that commit alone.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:high High risk and removed kronos:review:working review: in progress labels Nov 23, 2026
@quantconnect-kronos quantconnect-kronos Bot added kronos:develop:idle develop: paused and removed kronos:develop:working develop: in progress labels Nov 23, 2026
@jhonabreul

Copy link
Copy Markdown
Collaborator

#167 was merged. Update this branch

@quantconnect-kronos quantconnect-kronos Bot added kronos:develop:queued develop: queued kronos:develop:working develop: in progress and removed kronos:develop:idle develop: paused kronos:develop:queued develop: queued labels Nov 30, 2026
….NET

When .NET constructs a generated exception subclass, its Python object is
created before the base constructor runs, so its args and __cause__ describe
the exception before Message and InnerException were set. Set them again
once the base constructor has run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos
quantconnect-kronos Bot force-pushed the kronos/bug-168-refresh-dotnet-exception-args branch from 5f4ebf3 to d98d46f Compare November 30, 2026 14:04
@quantconnect-kronos

Copy link
Copy Markdown
Author

Rebased onto master: the branch is now only the fix commit, d98d46f, unchanged from before. Its test still fails on master without the fix and passes with it.

@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 30, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 30, 2026
@quantconnect-kronos-reviewer
quantconnect-kronos-reviewer Bot marked this pull request as ready for review November 30, 2026 14:25

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving. The rebase left the fix unchanged, and the branch is now just that commit on master, which resolves my earlier request.

  • With only ClassDerived.cs reverted to master, the new test fails as #168 describes. With the fix, tests/test_exceptions.py passes (33 tests).
  • Re-checked on the new base: args reflects get_Message overrides, including one inherited through two generated levels. A .NET-created instance raised and caught in Python keeps its args and __cause__, and 2,000 .NET-created instances are collected cleanly.
  • A correction to my earlier review, not blocking: with #167 merged, master no longer aborts when a get_Message override fails during construction from .NET. See the inline note.

I set risk:low: it's one small, easily reverted change on the .NET construction path of generated exception subclasses. A test fails without it, CI is green including the Lean jobs, and it was tested end to end.

Comment thread src/runtime/Types/ClassDerived.cs Resolved
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:low Low risk and removed risk:high High risk kronos:review:working review: in progress labels Nov 30, 2026
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 30, 2026
@jhonabreul

Copy link
Copy Markdown
Collaborator

I took ownership of this PR and I am answering the open decision for Martin, about #174 (review).

Decision: neither A nor B. Accept it as a known limit.

An outer exception and an inner exception that points back to it (for example through inner.Data) staying alive together is accepted. It needs very specialized code that regular users don't write, and an exception constructed from Python already behaves this way on master.

Keep the PR as simple as possible while fixing the original issue. No wrapper lifecycle change, here or as a prerequisite.

Developer, please: move the "Open decision" in the description to "Known limits". No code changes for it.

Reviewers: do not block on this case or on other cases that need this kind of specialized code. Report them as non-blocking.

@quantconnect-kronos

Copy link
Copy Markdown
Author

Done: the back-reference case is now under Known limits in the description, linking your decision, and the "Open decision" section is removed. No code changes.

@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 30, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 30, 2026
@quantconnect-kronos-reviewer
quantconnect-kronos-reviewer Bot marked this pull request as ready for review November 30, 2026 15:48

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving 30aa2d0. It fixes the regressions the last change request found in d0b9c19, and I found no new ones. It still does what #168 asks.

I compared head with master (Python 3.11 / .NET 10), using exceptions constructed from .NET with Activator.CreateInstance:

  • A .NET base Message that throws once the constructor has run: construction succeeds on both, and args is ('pre',) on both.
  • A .NET base Message that passes this to a Python delegate: construction succeeds on both with the same args. Reading Message afterwards passes the same Python object on both.
  • A .NET base Message that runs garbage collection: the object stays valid. Head gives ('msg',), master the default message.
  • new Exception("outer", <generated exception created without a constructor>): ('outer',) with a cause of the generated type, on both.
  • A Python-created exception whose get_Message counts its calls: ('2',) on both.
  • 15,000 constructions with an unpaired UTF-16 surrogate in the message: all succeed, and memory retained is the same on both.
  • 5,000 outer/inner pairs, with a generated inner exception: head gives args == ('o<i>',) and __cause__ is inner. Once queued finalizers are drained, all 10,000 objects are collected on both builds, with the same memory retained.
  • The #168 case, and a generated cause raised and caught in Python, give the right args and the cause by identity on head. master gives no cause.

pytest --runtime netcore tests gives 493 passed, 23 skipped on head. The new test fails on master.

I set risk:low in place of risk:high. Each behavior the earlier risk:high named, a constructor that throws, an invalid object, and an abort on a cause without a Python object, now matches master. The back-reference lifetime case is a known limit a maintainer accepted. The change is small, easy to revert and limited to generated exception subclasses. Its test fails without it, CI is green including the Lean jobs, and it was tested end to end.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:low Low risk and removed risk:high High risk kronos:review:working review: in progress labels Nov 30, 2026
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 30, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued and removed kronos:review Request review labels Nov 30, 2026
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added kronos:review:working review: in progress and removed kronos:review:queued review: queued labels Nov 30, 2026

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Requesting changes: the post-constructor refresh can deadlock while reading Message, or throw and retain objects while converting InnerException. In both cases the same constructor returns on master. I verified small fixes for both without changing wrapper ownership.

The #168 backtest reached algorithm completion on this head with the updated args, the original generated cause, and working delayed __init__. On master, args still held the default message and the cause was absent. Both runs hit the same separate shutdown error. The local Python suite passed (493 passed, 23 skipped).

I’m setting risk:high because the refresh can hang or fail otherwise successful .NET construction. The maintainer-accepted back-reference case is not a blocker.

Comment thread src/runtime/Types/ClassDerived.cs Resolved
Comment thread src/runtime/Exceptions.cs Resolved
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added risk:high High risk and removed risk:low Low risk kronos:review:working review: in progress labels Nov 30, 2026
…t convert the inner exception

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quantconnect-kronos
quantconnect-kronos Bot marked this pull request as draft November 30, 2026 16:03
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 30, 2026
@jhonabreul

Copy link
Copy Markdown
Collaborator

Decision: this PR is done at e109105. No more changes, and no more change requests for cases like the ones raised so far.

#168 is fixed: a generated exception constructed from .NET now has the right args and __cause__. CI is green on e109105, including the Lean jobs.

Why we stop here:

  • The whole path is already rare. To reach it, code has to define a __namespace__ subclass of a .NET exception and then have .NET construct it through reflection (Activator.CreateInstance, ConstructorInfo.Invoke) instead of calling it from Python. Lean algorithms don't do that.
  • The cases requested since are rarer still: a custom C# exception whose Message getter waits on another thread, one whose Message throws, a reflected two-step __init__, a message with an unpaired surrogate. Nobody writes this, and it will not happen in real use.
  • Each round has made the diff bigger and has started to touch code every wrapped .NET object goes through (CLRObject.Create, Exceptions.SetArgsAndCause). That is more risk than these cases are worth.

This replaces the earlier rule for this PR. I said a regression against master always blocks. For this PR it no longer does when the code needed to trigger it is like the cases above.

Developer, please:

  1. Keep e109105 as it is. Do not push more code or tests.
  2. If a new finding comes in that needs this kind of code, do not fix it. Add one line for it under "Known limits" in the description and reply saying so.

Reviewers:

  • Review e109105 against Generated exceptions constructed from .NET retain stale args and omit their cause #168's "Cause and fix requirement" only.
  • Block only if the issue's reproduction fails, or if code a Lean algorithm would really write breaks: a __namespace__ exception constructed from Python, raised, caught and read.
  • Anything that needs a custom C# exception class, reflection beyond the plain constructor call, threads, forced garbage collection, or unusual Message or args behaviour is not blocking. Report it as a known limit in one line, with no change request and no new issue.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving e109105. It fixes both regressions found in 30aa2d0, and in each case the result now matches master. I found nothing else in it that changes behavior.

I compared head with master (Python 3.11 / .NET 10), constructing __namespace__ exception subclasses with Activator.CreateInstance:

  • A .NET base Message that waits on a worker running a Python delegate: construction returns in about 10 ms on both builds, and head stores the worker's result in args.
  • A CLR inner exception whose Message throws: construction succeeds on both builds without a __cause__, and the inner exception is collected on both. Head's args holds the constructor's message.
  • A message plus a generated inner exception that has a Python attribute: head gives ('out',) and __cause__ is inner, with the attribute kept. master gives the default message and no cause.

Releasing the GIL around the Message read matches what MethodBinder already does by default around .NET calls made from Python. The using in CLRObject.Create only matters when SetArgsAndCause throws. On master, that case kept the wrapper and its exception alive.

tests/test_exceptions.py and tests/test_subclass.py pass on head (47 tests). CI is green, including the Lean regression and unit test jobs, which run this head's Python.Runtime.dll.

I set risk:low in place of risk:high. The two problems behind the last risk:high, a construction that hangs and one that fails, now behave as on master. The change is small, easy to revert and limited to exceptions. Its test fails on master, and it was tested end to end on 30aa2d0.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Requesting changes: keep __cause__ readable when Python constructs, raises, and catches a generated exception whose cause chain contains an exception retained by .NET. After ordinary .NET collection, the same code reads the inner message on master but raises TypeError: invalid target here. It needs no custom CLR type, reflection, worker thread, forced collection, or overrides.

Tested end to end: #168 now gives the constructor message in args and preserves the generated cause by identity. The Python-created case below fails on this head and passes on master and with the small fallback guard. I'm setting risk:high because the existing Python exception-handling path can no longer read a previously readable cause.

Non-blocking known limit: a custom CLR inner exception whose Message waits for a Python callback can still stall while the cause is wrapped; this does not affect the ordinary constructor case.

Comment thread src/runtime/Exceptions.cs Resolved
…LR wrapper

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving e734894. It fixes the regression the last change request found in e109105. When a generated inner exception's CLR wrapper has been collected, __cause__ now gets a new wrapper, as on master. I found no new regression.

What I compared across head, master and e109105 (Python 3.11 / .NET 10):

  • The change request's repro: raise Error("workflow failed", failed.Exception), with failed = Task.FromException(Error("inner")), after ordinary allocations. __cause__.__cause__.Message is 'inner' on head and on master. e109105 raises TypeError: invalid target.
  • The same inner exception wrapped by a plain System.Exception("outer", failed.Exception): the cause reads the inner message on head and on master. e109105 raises the same TypeError.
  • A live generated inner exception: outer.__cause__ is inner on head, keeping its attributes. master gives a second wrapper without them.
  • #168's case, Activator.CreateInstance(Error, ["custom message", inner]): head gives ('custom message',) with __cause__ is inner. master gives the default message and no cause. Across 2,000 such pairs, with collections in between, all match on head.
  • tests/test_exceptions.py and tests/test_subclass.py: 47 passed on head. CI is green on head, including the Lean jobs.

Non-blocking: the fix in SetArgsAndCause has no test in the suite. If the decision against more tests changes, the change request's repro would make one.

Not caused by this change: on master and head alike, reading failed.Exception.InnerException.Message from Python raises TypeError: invalid target. Any __namespace__ object that only .NET holds loses its CLR wrapper after a .NET collection. Opened #187 for it.

I'm keeping risk:high, in place of the risk:low of my last review. My approval of e109105 missed the regression above. The change now affects how every .NET exception with an inner exception is converted to Python. Its end-to-end run was on 30aa2d0, before the last two commits. An end-to-end run on this head that covers #168's case and a chained exception raised from Python would lower it.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approving. Tested this head end to end: a generated exception constructed from .NET keeps the constructor message in args and preserves a live generated cause by identity after raise/catch. A Python-constructed, raised and caught exception again exposes the message of a cause held only by .NET, matching master; e109105 raised TypeError: invalid target. All three runs completed the algorithm checks before the same unrelated shutdown error. The Python suite passed locally (493 passed, 23 skipped).

The inline note records a non-blocking fallback lifetime limit: the new .NET-construction path can retain replacement cause wrappers. It does not worsen the Python-construction path. risk:high stays.

Comment thread src/runtime/Exceptions.cs
: null;
using var cause = wrapper != null
? ClassDerivedObject.ToPython((IPythonDerivedType)e.InnerException)
: CLRObject.GetReference(e.InnerException);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Known limit, not blocking: ordinary .NET GC can collect a generated inner exception's weak CLR wrapper before Activator.CreateInstance constructs the outer exception. The fallback makes the cause readable, but the replacement isn't the peer that the CLR finalizer frees. After dropping the outers and draining finalizers, 20,000 constructions retained about 4.15 MB here versus effectively zero on master, which left the cause unset. Python-created outers using the same fallback retained the same amount on both. Please record the .NET-construction case under Known limits; no wrapper lifecycle change requested.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Recorded under Known limits, per the maintainer decision on exotic cases, with no code change.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kronos:develop:working develop: in progress risk:high High risk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated exceptions constructed from .NET retain stale args and omit their cause

1 participant