Bug 47388 - Memory leak when using FromSynchronizationContext
Summary: Memory leak when using FromSynchronizationContext
Alias: None
Product: Runtime
Classification: Mono
Component: GC ()
Version: 4.6.0 (C8)
Hardware: All Linux
: --- normal
Target Milestone: ---
Assignee: Vlad Brezae
Depends on:
Reported: 2016-11-18 18:16 UTC by Brandon White
Modified: 2017-01-13 01:09 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Minimally reproducing program (3.34 KB, application/x-7z-compressed)
2016-11-18 18:16 UTC, Brandon White

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Brandon White 2016-11-18 18:16:01 UTC
Created attachment 18543 [details]
Minimally reproducing program

When using TaskScheduler.FromSynchronizationContext() frequently, memory is leaked very slowly over time.

This appears to be due to TaskScheduler.s_activeTaskSchedulers retaining entries for TaskSchedulers that are no longer in use.  Whether this is due to ConditionalWeakTable or something else holding on to a reference has yet to be determined.

Attached is a distilled program for reproducing the issue.  Running the program will cause TaskScheduler.FromCurrentSynchronizationContext() to be called roughly every millisecond as part of a Task.Delay.ContinueWith tail-recursive type loop.

If you comment out line 63 and uncomment line 64, you can then observe that the leak does not happen.

We have currently reproduced this on an ARM embedded system as well as an Ubuntu PC, both using Mono 4.2.1.

I will be following up with a test of the latest stable Mono version.
Comment 1 Brandon White 2016-12-01 15:32:05 UTC
I can now confirm that I have reproduced this in 4.6.2.

Mono JIT compiler version 4.6.2 (Stable Mon Nov 14 12:30:00 UTC 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen
Comment 2 Vlad Brezae 2017-01-13 01:09:24 UTC
Fixed on mono master by a3b053bacf4436f213391b9776d2ab89b8cf3ff7.