Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Fix memory leak when serving static content #2880

Open
wants to merge 1 commit into
base: 1.x-WorkingBranch
Choose a base branch
from

Conversation

jeffthiele
Copy link
Contributor

@jeffthiele jeffthiele commented Mar 20, 2018

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the Nancy code style guidelines
  • I have provided test coverage for my change (where applicable)

Description

Added calls to dispose the CancellationTokenSource when the engine returns static content and on errors. Potentially fixes the memory leaks described in #2605 without reverting the fix in #2150

@dnfclas
Copy link

dnfclas commented Mar 20, 2018

CLA assistant check
All CLA requirements met.

{
context.Response = staticContentResponse;
tcs.SetResult(context);
return tcs.Task;
Copy link

Choose a reason for hiding this comment

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

Does the TaskCompletionSource use or reference the Cancellation Token? I don't quite understand how the
try ... finally { cts.Dispose() } is any different than

cts.Dispose(); 
return tsc.Task;

This isn't dependent on the asynchronous execution of anything, unlike the lifeCycleTask below, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only difference is that try{...}finally { cts.Dispose();} will still dispose of the CancellationTokenSource whether an exception occurs on the intervening lines or not.

Choose a reason for hiding this comment

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

Ah, going for the equivalent behavior of declaring a using block inside this scope, and there is some possibility of an exception setting the result or the response. That makes more sense to me.

Choose a reason for hiding this comment

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

In theory each of the individual try...finally... could be replaced with separate using (cts) {...} right? (Just working out some personal edification here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In these cases, yes, I think it would be functionally the same. Personally, I like being more explicit that we're only disposing of the item here since the typical use of using also instantiates the object and could potentially be misinterpreted by others looking at the code.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants