Skip to content

Conversation

@inklesspen1rus
Copy link

Multiple disposes are common thing in .NET. MSDN says that too:

Dispose() can be called multiple times by other objects. (link)

To help ensure that resources are always cleaned up appropriately, a Dispose method should be idempotent, such that it's callable multiple times without throwing an exception. Furthermore, subsequent invocations of Dispose should do nothing. (link)

You should be safe to call it more than once, though you should probably avoid it if you can. (community, link)

Critical events may spuriously fill up event log with critical events while operations like that aren't even logical error.

So I switched it's event level to Verbose. I don't think there are reasons to use Information for events like that, when AspNetCore disposes streams twice.

Closes #394

@inklesspen1rus
Copy link
Author

@microsoft-github-policy-service agree

@benmwatson
Copy link
Member

benmwatson commented Dec 4, 2025

I'm convinced. I just checked and internally, we don't even track this metric. Verbose makes sense.

However, I think you should make one more modification. See code review comment.

Copy link
Member

@benmwatson benmwatson left a comment

Choose a reason for hiding this comment

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

The Dispose(bool) method call is grabbing a stack trace, and this will only make sense now if Verbose logging is enabled. Can you modify the code like this in Dispose?

if (this.memoryManager.options.GenerateCallStacks && Events.Log.IsEnabled(EventLevel.Verbose))
...

@inklesspen1rus
Copy link
Author

@benmwatson

AllocationStack is used for StreamDoubleDisposed event so we need to check this event too, but we can check the event only in RecyclableMemoryStreamManager. So I dedicated internal property to check if we need generate stacktrace.

Ideally I'd move stacktrace generation into ReportStreamDoubleDisposed but that changes behaviour (adds one more frame into stacktrace).

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.

Consider changing log severity of double dispose so not critical

2 participants