-
Notifications
You must be signed in to change notification settings - Fork 577
/
CrossAppDomainCleanUp.cs
611 lines (557 loc) · 23.4 KB
/
CrossAppDomainCleanUp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RazorEngine.Compilation
{
/// <summary>
/// Helper class to cleanup locked files and folders after the current AppDomain has been unloaded.
/// (Because of locking they can't be deleted while the current AppDomain is up, see https://github.com/Antaris/RazorEngine/issues/244)
/// </summary>
public class CrossAppDomainCleanUp : IDisposable
{
/// <summary>
/// Simple helper object to print status messages across appdomains.
/// </summary>
public interface IPrinter
{
/// <summary>
/// Print a status message
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
void Print(string format, params object[] args);
/// <summary>
/// Print a error message.
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
void PrintError(string format, params object[] args);
}
/// <summary>
/// A simple Printer which wrints in stdout and stderr.
/// </summary>
public class Printer : CrossAppDomainObject, IPrinter
{
/// <summary>
/// Creates a new simple printer.
/// </summary>
public Printer()
{
}
void IPrinter.Print(string format, params object[] args)
{
Console.WriteLine(format, args);
}
void IPrinter.PrintError(string format, params object[] args)
{
Console.Error.WriteLine(format, args);
}
}
/// <summary>
/// A simple printer writing only in stderr.
/// </summary>
[Serializable]
public class ErrorOnlyPrinter : IPrinter
{
/// <summary>
/// Create a new ErrorOnlyPrinter.
/// </summary>
public ErrorOnlyPrinter()
{
}
void IPrinter.Print(string format, params object[] args)
{
}
void IPrinter.PrintError(string format, params object[] args)
{
Console.Error.WriteLine(format, args);
}
}
/// <summary>
/// A new empty printer, which prints nothing.
/// </summary>
[Serializable]
public class EmptyPrinter : IPrinter
{
/// <summary>
/// Creates a new EmptyPrinter
/// </summary>
public EmptyPrinter()
{
}
void IPrinter.Print(string format, params object[] args)
{
}
void IPrinter.PrintError(string format, params object[] args)
{
}
}
/// <summary>
/// Helper class to communicate with the Cleanup AppDomain.
/// Inits the cleanup AppDomain (AssemblyResolve) and registers items to delete.
/// </summary>
[SecurityCritical]
public class CleanupHelper : global::RazorEngine.CrossAppDomainObject
{
/// <summary>
/// A helper to be able to subscribe to the DomainUnload event.
/// Additionally we use this object to check if the printer lives in the wrong appdomain
/// (which would lead to an application crash).
/// </summary>
[SecurityCritical]
public class SubscribeHelper : CrossAppDomainObject
{
private CleanupHelper _helper = null;
/// <summary>
/// Subscribe to the DomainUnload event and call the helper back.
/// </summary>
/// <param name="helper"></param>
public void Subscribe(CleanupHelper helper)
{
if (helper == null)
{
throw new ArgumentNullException("helper");
}
_helper = helper;
SubscribePrivate();
}
/// <summary>
/// Check if the given printer object is valid.
/// </summary>
/// <param name="printer"></param>
public void CheckPrinter(IPrinter printer)
{
CheckPrinterPrivate(printer);
}
[SecuritySafeCritical]
private void CheckPrinterPrivate(IPrinter printer)
{
(new PermissionSet(PermissionState.Unrestricted)).Assert();
if (!System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain(printer) ||
!System.Runtime.Remoting.RemotingServices.IsTransparentProxy(printer))
{
throw new InvalidOperationException("Printer lives in the appdomain which we wait to exit (printer will be dead when we need it)!");
}
}
[SecuritySafeCritical]
private void SubscribePrivate()
{
(new PermissionSet(PermissionState.Unrestricted)).Assert();
AppDomain.CurrentDomain.DomainUnload += domain_DomainUnload;
}
void domain_DomainUnload(object sender, EventArgs e)
{
if (_helper != null)
{
_helper.domain_DomainUnload(sender, e);
}
}
}
/// <summary>
/// Check if the given AppDomain is unloaded.
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
public static bool IsUnloaded(AppDomain domain)
{
Action<string> ignore = z => { };
try
{
ignore(domain.FriendlyName);
return false;
}
catch (System.Threading.ThreadAbortException)
{ // Mono bug, it throws ThreadAbortExceptions a lot more aggressively
// We will shutdown after cleanup, se leave us alone.
Thread.ResetAbort();
return true;
}
catch (System.Runtime.Remoting.RemotingException)
{ // Mono bug, should throw AppDomainUnloadedException instead
return true;
}
catch (AppDomainUnloadedException)
{
return true;
}
}
private readonly ConcurrentQueue<string> _toCleanup = new ConcurrentQueue<string>();
private AppDomain _domain;
private IPrinter _printer;
private void CheckInit()
{
if (_domain == null)
{
throw new InvalidOperationException("You need to init me first!");
}
}
/// <summary>
/// Init the current helper object with the given AppDomain.
/// </summary>
/// <param name="domain"></param>
/// <param name="printer"></param>
[SecurityCritical]
public void Init(AppDomain domain, IPrinter printer = null)
{
(new PermissionSet(PermissionState.Unrestricted)).Assert();
if (_domain != null)
{
throw new InvalidOperationException("CleanupHelper can be used only for a single domain.");
}
if (domain == null)
{
throw new ArgumentNullException("domain");
}
if (printer == null)
{
printer = new ErrorOnlyPrinter();
}
_printer = printer;
if (domain.IsDefaultAppDomain())
{
//throw new InvalidOperationException("CleanupHelper will not work on the default AppDomain")
printer.PrintError("We can't cleanup temp files if you use RazorEngine on the default Appdomain.");
printer.PrintError("Create a new AppDomain and use RazorEngine from there!");
}
printer.Print("init cleanup helper for {0} in {1} ...", domain.FriendlyName, AppDomain.CurrentDomain.FriendlyName);
this._domain = domain;
//_domain.DomainUnload += domain_DomainUnload;
ObjectHandle handle =
Activator.CreateInstanceFrom(
_domain, typeof(SubscribeHelper).Assembly.ManifestModule.FullyQualifiedName,
typeof(SubscribeHelper).FullName
);
using (var subscribeHelper = (SubscribeHelper)handle.Unwrap())
{
if (System.Runtime.Remoting.RemotingServices.IsTransparentProxy(printer))
{
subscribeHelper.CheckPrinter(printer);
}
subscribeHelper.Subscribe(this);
}
}
private bool DoCleanUp()
{
bool success = true;
string item;
var failedFiles = new Queue<string>();
while (_toCleanup.TryDequeue(out item))
{
Exception error = null;
if (File.Exists(item))
{
try
{
File.Delete(item);
}
catch (IOException exn)
{
error = exn;
}
catch (UnauthorizedAccessException exn)
{
error = exn;
}
}
if (Directory.Exists(item))
{
try
{
Directory.Delete(item, true);
}
catch (IOException exn)
{
error = exn;
}
catch (UnauthorizedAccessException exn)
{
error = exn;
}
}
if (error != null)
{
success = false;
failedFiles.Enqueue(item);
_printer.PrintError("Could not delete {0}: {1}", item, error.ToString());
}
}
while (failedFiles.Count > 0)
{
_toCleanup.Enqueue(failedFiles.Dequeue());
}
return success;
}
[SecurityCritical]
void domain_DomainUnload(object sender, EventArgs e)
{
var senderDomain = (AppDomain)sender;
var friendlyName = senderDomain.FriendlyName;
_printer.Print("Domain {0} is unloading starting task...", friendlyName);
// NOTE: a background thread or task will get killed before it can finish the work!
var t = new Thread(() =>
{
try
{
_printer.Print("waiting a bit for {0}...", friendlyName);
while (!IsUnloaded(senderDomain))
{
Thread.Sleep(100);
}
// HACK: Race condition because there is no way to detect if an AppDomain is fully unloaded.
Thread.Sleep(200);
_printer.Print("cleanup after {0}...", friendlyName);
// Because AppDomain could still be up we try
int max_tries = 10;
while (!DoCleanUp() && max_tries > 0)
{
max_tries--;
if (max_tries > 0)
{
_printer.Print("Could not delete everything of {0}, trying again...", friendlyName);
Thread.Sleep(500);
}
else
{
_printer.PrintError("Could not clean up {0}, giving up...", friendlyName);
}
}
_printer.Print("unload ourself {0}...", AppDomain.CurrentDomain.FriendlyName);
}
catch (Exception exn)
{
try
{
_printer.PrintError("Unhandled error while cleaning up after {0}: {1}", friendlyName, exn.ToString());
}
catch (Exception pExn)
{
// Write in console just in case our printer has died.
System.Console.Error.WriteLine("RazorEngine: Error in cleanup code: {0}", exn);
System.Console.Error.WriteLine("RazorEngine: Printer has died as well: {0}", pExn);
}
}
// We are not needed anymore.
//AppDomain.Unload(AppDomain.CurrentDomain);
});
t.IsBackground = false;
t.Start();
}
/// <summary>
/// Register the given path for cleanup.
/// </summary>
/// <param name="path"></param>
public void RegisterCleanupPath(string path)
{
_printer.Print("Register cleanup path {0} ...", path);
CheckInit();
_toCleanup.Enqueue(path);
}
/// <summary>
/// Setup AssemblyResolve.
/// </summary>
[SecurityCritical]
public void SetupDomain()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var name = Assembly.GetExecutingAssembly().GetName();
if (args.Name == name.FullName)
{
return Assembly.GetExecutingAssembly();
}
return null;
}
}
private static IPrinter currentPrinter = new ErrorOnlyPrinter();
private static Lazy<CrossAppDomainCleanUp> cleanup = new Lazy<CrossAppDomainCleanUp>(CreateInitial);
private static bool writtenLongMessage = false;
/// <summary>
/// Gets or sets the printer that is used by default when creating new CrossAppDomainCleanUp objects.
/// Do not use this property unless you know what you are doing.
/// Settings this to a serializable object is safe, however setting this to a marshalbyrefobject
/// can lead to errors if the object lives in the domain that is watched for unloading
/// </summary>
public static IPrinter CurrentPrinter { get { return currentPrinter; } set { currentPrinter = value; } }
/// <summary>
/// A cleanup instance for the current AppDomain
/// </summary>
public static CrossAppDomainCleanUp CurrentCleanup { get { return cleanup.Value; } }
[SecuritySafeCritical]
private static CrossAppDomainCleanUp CreateInitial()
{
return new CrossAppDomainCleanUp(AppDomain.CurrentDomain, currentPrinter);
}
/// <summary>
/// Get the StrongName of the given assembly.
/// </summary>
/// <param name="ass"></param>
/// <returns></returns>
public static StrongName FromAssembly(Assembly ass)
{
var name = ass.GetName();
byte[] pk = name.GetPublicKey();
var blob = new StrongNamePublicKeyBlob(pk);
return new StrongName(blob, name.Name, name.Version);
}
/// <summary>
/// A helper method to register items to cleanup for the current AppDomain.
/// </summary>
/// <param name="item"></param>
/// <param name="throwOnDefault">Throw an exception when we are on the default AppDomain</param>
public static void RegisterCleanup(string item, bool throwOnDefault = true)
{
if (Type.GetType("Mono.Runtime") != null)
{
// our cleanup logic doesn't work on mono (mono bugs)
// We don't need it on unix, as we can just delete the files there
// (handled by the caller)
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
{
return;
}
System.Console.Error.WriteLine("Mono can't handle our cleanup logic, so please report crashes to mono.", item);
}
if (AppDomain.CurrentDomain.IsDefaultAppDomain())
{
if (!writtenLongMessage)
{
System.Console.Error.WriteLine("RazorEngine: We can't cleanup temp files if you use RazorEngine on the default Appdomain.");
System.Console.Error.WriteLine("Create a new AppDomain and use RazorEngine from there.");
System.Console.Error.WriteLine("Read the quickstart or https://github.com/Antaris/RazorEngine/issues/244 for details!");
System.Console.Error.WriteLine("You can ignore this and all following 'Please clean ... manually' messages if you are using DisableTempFileLocking, which is not recommended.");
writtenLongMessage = true;
}
if (throwOnDefault)
{
throw new InvalidOperationException("Cleanup doesn't work for the default AppDomain.");
}
else
{
System.Console.Error.WriteLine("Please clean '{0}' manually!", item);
return;
}
}
CurrentCleanup.RegisterCleanupPath(item);
}
private readonly AppDomain _domain;
private readonly CleanupHelper _helper;
/*
[SecurityCritical]
public static AppDomain GetDefaultAppDomain()
{
(new PermissionSet(PermissionState.Unrestricted)).Assert();
if (AppDomain.CurrentDomain.IsDefaultAppDomain())
{
return AppDomain.CurrentDomain;
}
var isMono = Type.GetType("Mono.Runtime") != null;
if (isMono)
{
var prop = typeof(System.AppDomain).GetProperty("DefaultDomain", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
return (AppDomain)prop.GetMethod.Invoke(null, null);
}
else
{
mscoree.ICorRuntimeHost host = new mscoree.CorRuntimeHostClass();
object domain;
host.GetDefaultDomain(out domain);
return (AppDomain)domain;
}
}*/
/// <summary>
/// Create a new CrossAppDomainCleanUp object for the current AppDomain.
/// </summary>
/// <param name="toWatch">the appDomain to watch for unload.</param>
/// <param name="printer"></param>
[SecurityCritical]
public CrossAppDomainCleanUp(AppDomain toWatch, IPrinter printer)
{
if (toWatch == null) throw new ArgumentNullException("toWatch");
if (printer == null) throw new ArgumentNullException("printer");
(new PermissionSet(PermissionState.Unrestricted)).Assert();
var current = toWatch;
AppDomainSetup adSetup = new AppDomainSetup();
adSetup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//#if RAZOR4 // currently not signed!
var strongNames = new StrongName[0];
//#else
// var strongNames = new[] {
// FromAssembly(typeof(RazorEngine.Templating.RazorEngineService).Assembly),
// FromAssembly(typeof(System.Web.Razor.RazorTemplateEngine).Assembly)
// };
//#endif
_domain = AppDomain.CreateDomain(
"CleanupHelperDomain_" + Guid.NewGuid().ToString(), null,
current.SetupInformation, new PermissionSet(PermissionState.Unrestricted),
strongNames);
var initHelper = new InitHelper() { Domain = _domain, Current = current };
_helper = ExecutionContextLessThread.DefaultCallFunc(new Func<CleanupHelper>(initHelper.CreateHelper));
}
/// <summary>
/// This class only exists because we can't use a simple lambda.
/// </summary>
[SecuritySafeCritical]
private class InitHelper
{
internal AppDomain Domain { get; set; }
internal AppDomain Current { get; set; }
[SecuritySafeCritical]
internal CleanupHelper CreateHelper()
{
(new PermissionSet(PermissionState.Unrestricted)).Assert();
ObjectHandle handle =
Activator.CreateInstanceFrom(
Domain, typeof(CleanupHelper).Assembly.ManifestModule.FullyQualifiedName,
typeof(CleanupHelper).FullName
);
var helper = (CleanupHelper)handle.Unwrap();
helper.SetupDomain();
helper.Init(Current, CrossAppDomainCleanUp.CurrentPrinter);
return helper;
}
}
/// <summary>
/// This class only exists because we can't use a simple lambda.
/// </summary>
[SecuritySafeCritical]
private class RegisterCleanupHelper
{
internal string Path { get; set; }
internal CleanupHelper Helper { get; set; }
[SecuritySafeCritical]
internal void RegisterCleanupPath (){
Helper.RegisterCleanupPath(Path);
}
}
/// <summary>
/// Register the given path for cleanup.
/// </summary>
/// <param name="path"></param>
[SecuritySafeCritical]
public void RegisterCleanupPath(string path)
{
var caller = new RegisterCleanupHelper() { Helper = _helper, Path = path };
ExecutionContextLessThread.DefaultCallAction(caller.RegisterCleanupPath);
}
/// <summary>
/// Dispose the current instance.
/// </summary>
[SecuritySafeCritical]
public void Dispose()
{
ExecutionContextLessThread.DefaultCallAction(_helper.Dispose);
}
}
}