Skip to content

Commit acb934e

Browse files
committed
ensures timer's are not GCd
1 parent 3aa486a commit acb934e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/Umbraco.Web/Scheduling/Scheduler.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ namespace Umbraco.Web.Scheduling
2020
/// </remarks>
2121
internal sealed class Scheduler : ApplicationEventHandler
2222
{
23-
private Timer _pingTimer;
24-
private Timer _schedulingTimer;
25-
private LogScrubber _scrubber;
23+
private static Timer _pingTimer;
24+
private static Timer _schedulingTimer;
25+
private static LogScrubber _scrubber;
2626

2727
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
2828
{
@@ -37,12 +37,18 @@ protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplica
3737
// just copied over here for backward compatibility
3838
// of course we should have a proper scheduler, see #U4-809
3939

40-
// ping/keepalive
41-
_pingTimer = new Timer(KeepAlive.Start, applicationContext, 60000, 300000);
40+
//NOTE: It is important to note that we need to use the ctor for a timer without the 'state' object specified, this is in order
41+
// to ensure that the timer itself is not GC'd since internally .net will pass itself in as the state object and that will keep it alive.
42+
// There's references to this here: http://stackoverflow.com/questions/4962172/why-does-a-system-timers-timer-survive-gc-but-not-system-threading-timer
43+
// we also make these timers static to ensure further GC safety.
4244

43-
// scheduled publishing/unpublishing
45+
// ping/keepalive
46+
_pingTimer = new Timer(KeepAlive.Start);
47+
_pingTimer.Change(60000, 300000);
4448

45-
_schedulingTimer = new Timer(PerformScheduling, applicationContext, 30000, 60000);
49+
// scheduled publishing/unpublishing
50+
_schedulingTimer = new Timer(PerformScheduling);
51+
_schedulingTimer.Change(30000, 60000);
4652

4753
//log scrubbing
4854
_scrubber = new LogScrubber();

0 commit comments

Comments
 (0)