forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDispose.cs
More file actions
513 lines (443 loc) · 22.9 KB
/
Dispose.cs
File metadata and controls
513 lines (443 loc) · 22.9 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Win32.SafeHandles;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace System.IO.Tests
{
[Collection(nameof(DisableParallelization))] // make sure no other tests are calling GC.Collect()
public class FileStream_Dispose : FileSystemTest
{
[Fact]
public void DisposeClosesHandle()
{
SafeFileHandle handle;
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
handle = fs.SafeFileHandle;
}
Assert.True(handle.IsClosed);
}
private class MyFileStream : FileStream
{
public MyFileStream(string path, FileMode mode)
: base(path, mode)
{ }
public MyFileStream(SafeFileHandle handle, FileAccess access, Action<bool> disposeMethod) : base(handle, access)
{
DisposeMethod = disposeMethod;
}
public Action<bool> DisposeMethod { get; set; }
protected override void Dispose(bool disposing)
{
Action<bool> disposeMethod = DisposeMethod;
if (disposeMethod != null)
disposeMethod(disposing);
base.Dispose(disposing);
}
}
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void Dispose_CallsVirtualDisposeTrueArg_ThrowsDuringFlushWriteBuffer_DisposeThrows()
{
RemoteExecutor.Invoke(() =>
{
string fileName = GetTestFilePath();
using (FileStream fscreate = new FileStream(fileName, FileMode.Create))
{
fscreate.WriteByte(0);
}
bool writeDisposeInvoked = false;
Action<bool> writeDisposeMethod = _ => writeDisposeInvoked = true;
using (var fsread = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
Action act = () => // separate method to avoid JIT lifetime-extension issues
{
using (var fswrite = new MyFileStream(fsread.SafeFileHandle, FileAccess.Write, writeDisposeMethod))
{
fswrite.WriteByte(0);
// Normal dispose should call Dispose(true). Throws due to FS trying to flush write buffer
Assert.Throws<UnauthorizedAccessException>(() => fswrite.Dispose());
Assert.True(writeDisposeInvoked, "Expected Dispose(true) to be called from Dispose()");
writeDisposeInvoked = false;
// Only throws on first Dispose call
fswrite.Dispose();
Assert.True(writeDisposeInvoked, "Expected Dispose(true) to be called from Dispose()");
writeDisposeInvoked = false;
}
Assert.True(writeDisposeInvoked, "Expected Dispose(true) to be called from Dispose() again");
writeDisposeInvoked = false;
};
act();
for (int i = 0; i < 2; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
Assert.False(writeDisposeInvoked, "Expected finalizer to have been suppressed");
}
}).Dispose();
}
private static bool IsPreciseGcSupportedAndRemoteExecutorSupported => PlatformDetection.IsPreciseGcSupported && RemoteExecutor.IsSupported;
[ConditionalFact(typeof(FileStream_Dispose), nameof(IsPreciseGcSupportedAndRemoteExecutorSupported))]
public void NoDispose_CallsVirtualDisposeFalseArg_ThrowsDuringFlushWriteBuffer_FinalizerWontThrow()
{
RemoteExecutor.Invoke(() =>
{
string fileName = GetTestFilePath();
using (FileStream fscreate = new FileStream(fileName, FileMode.Create))
{
fscreate.WriteByte(0);
}
bool writeDisposeInvoked = false;
Action<bool> writeDisposeMethod = (disposing) =>
{
writeDisposeInvoked = true;
Assert.False(disposing, "Expected false arg to Dispose(bool)");
};
using (var fsread = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
Action act = () => // separate method to avoid JIT lifetime-extension issues
{
var fswrite = new MyFileStream(fsread.SafeFileHandle, FileAccess.Write, writeDisposeMethod);
fswrite.WriteByte(0);
};
act();
// Dispose is not getting called here.
// instead, make sure finalizer gets called and doesnt throw exception
for (int i = 0; i < 2; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
Assert.True(writeDisposeInvoked, "Expected finalizer to be invoked but not throw exception");
}
}).Dispose();
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported))]
public void Dispose_CallsVirtualDispose_TrueArg()
{
bool disposeInvoked = false;
Action act = () => // separate method to avoid JIT lifetime-extension issues
{
using (MyFileStream fs = new MyFileStream(GetTestFilePath(), FileMode.Create))
{
fs.DisposeMethod = (disposing) =>
{
disposeInvoked = true;
Assert.True(disposing, "Expected true arg to Dispose(bool)");
};
// Normal dispose should call Dispose(true)
fs.Dispose();
Assert.True(disposeInvoked, "Expected Dispose(true) to be called from Dispose()");
disposeInvoked = false;
}
// Second dispose leaving the using should still call dispose
Assert.True(disposeInvoked, "Expected Dispose(true) to be called from Dispose() again");
disposeInvoked = false;
};
act();
// Make sure we suppressed finalization
for (int i = 0; i < 2; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
Assert.False(disposeInvoked, "Expected finalizer to have been suppressed");
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported), nameof(PlatformDetection.IsNotBrowser))]
public void Finalizer_CallsVirtualDispose_FalseArg()
{
bool disposeInvoked = false;
Action act = () => // separate method to avoid JIT lifetime-extension issues
{
new MyFileStream(GetTestFilePath(), FileMode.Create)
{
DisposeMethod = (disposing) =>
{
disposeInvoked = true;
Assert.False(disposing, "Expected false arg to Dispose(bool)");
}
};
};
act();
for (int i = 0; i < 2; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
Assert.True(disposeInvoked, "Expected finalizer to be invoked and set called");
}
[Fact]
public void DisposeFlushesWriteBuffer()
{
string fileName = GetTestFilePath();
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
fs.Write(TestBuffer, 0, TestBuffer.Length);
}
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
{
byte[] buffer = new byte[TestBuffer.Length];
Assert.Equal(buffer.Length, fs.Length);
fs.Read(buffer, 0, buffer.Length);
Assert.Equal(TestBuffer, buffer);
}
}
[Fact]
public void DerivedFileStream_PropertiesDontThrow_OnDispose()
{
var fs = new DerivedFileStreamAccessingPropertiesOnDispose(GetTestFilePath(), FileMode.Create);
fs.Dispose();
fs.VerifyAfterDispose();
}
public class DerivedFileStreamWithFinalizer : FileStream
{
public static int DisposeTrueCalled = 0;
public static int DisposeFalseCalled = 0;
public DerivedFileStreamWithFinalizer(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
: base(path, mode, access, share, bufferSize, options)
{
}
public DerivedFileStreamWithFinalizer(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
: base(handle, access, bufferSize, isAsync)
{
}
public DerivedFileStreamWithFinalizer(IntPtr handle, FileAccess access, bool ownsHandle)
#pragma warning disable CS0618 // Type or member is obsolete
: base(handle, access, ownsHandle)
#pragma warning restore CS0618 // Type or member is obsolete
{
}
~DerivedFileStreamWithFinalizer() => Dispose(false);
protected override void Dispose(bool disposing)
{
if (disposing)
{
DisposeTrueCalled++;
}
else
{
DisposeFalseCalled++;
}
base.Dispose(disposing);
}
}
public class DerivedFileStreamWithoutFinalizer : FileStream
{
public static int DisposeTrueCalled = 0;
public static int DisposeFalseCalled = 0;
public DerivedFileStreamWithoutFinalizer(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
: base(path, mode, access, share, bufferSize, options)
{
}
public DerivedFileStreamWithoutFinalizer(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
: base(handle, access, bufferSize, isAsync)
{
}
public DerivedFileStreamWithoutFinalizer(IntPtr handle, FileAccess access, bool ownsHandle)
#pragma warning disable CS0618 // Type or member is obsolete
: base(handle, access, ownsHandle)
#pragma warning restore CS0618 // Type or member is obsolete
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
DisposeTrueCalled++;
}
else
{
DisposeFalseCalled++;
}
base.Dispose(disposing);
}
}
public sealed class DerivedFileStreamAccessingPropertiesOnDispose : FileStream
{
private readonly string _name;
private bool _disposed;
public DerivedFileStreamAccessingPropertiesOnDispose(string path, FileMode mode) : base(path, mode, FileAccess.ReadWrite)
{
_name = path;
}
protected override void Dispose(bool disposing)
{
if (!_disposed)
{
Assert.Equal(_name, Name);
Assert.False(IsAsync);
Assert.True(CanRead);
Assert.True(CanSeek);
Assert.True(CanWrite);
Assert.False(CanTimeout);
Assert.Equal(0, Length);
Assert.Equal(0, Position);
#pragma warning disable CS0618 // Type or member is obsolete
Assert.NotEqual(nint.Zero, Handle);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.NotNull(SafeFileHandle);
_disposed = true;
}
base.Dispose(disposing);
}
public void VerifyAfterDispose()
{
Assert.True(_disposed, "This method must be called only after the object has been disposed.");
Assert.Throws<ObjectDisposedException>(() => Length);
Assert.Throws<ObjectDisposedException>(() => Position);
#pragma warning disable CS0618 // Type or member is obsolete
Assert.Throws<ObjectDisposedException>(() => Handle);
#pragma warning restore CS0618 // Type or member is obsolete
Assert.Throws<ObjectDisposedException>(() => SafeFileHandle);
Assert.False(CanRead);
Assert.False(CanSeek);
Assert.False(CanWrite);
Assert.False(CanTimeout);
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported), nameof(PlatformDetection.IsNotBrowser))]
public void FinalizeFlushesWriteBuffer()
=> VerifyFlushedBufferOnFinalization(
filePath => new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, bufferSize: 4096, useAsync: false));
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported), nameof(PlatformDetection.IsNotBrowser))]
public void FinalizeFlushesWriteBufferForDerivedFileStreamWithFinalizerCreatedFromPath()
=> VerifyFlushedBufferOnFinalization(
filePath => new DerivedFileStreamWithFinalizer(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, bufferSize: 4096, FileOptions.None));
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported), nameof(PlatformDetection.IsNotBrowser))]
public void FinalizeFlushesWriteBufferForDerivedFileStreamWithFinalizerCreatedFromSafeFileHandle()
=> VerifyFlushedBufferOnFinalization(
filePath => new DerivedFileStreamWithFinalizer(
File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, FileOptions.None),
FileAccess.Write, bufferSize: 4096, isAsync: false));
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported), nameof(PlatformDetection.IsNotBrowser))]
public void FinalizeFlushesWriteBufferForDerivedFileStreamWithFinalizerCreatedFromIntPtr()
=> VerifyFlushedBufferOnFinalization(
filePath => new DerivedFileStreamWithFinalizer(
File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, FileOptions.None).DangerousGetHandle(),
FileAccess.Write,
ownsHandle: true));
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported), nameof(PlatformDetection.IsNotBrowser))]
public void FinalizeFlushesWriteBufferForDerivedFileStreamWithoutFinalizerCreatedFromPath()
=> VerifyFlushedBufferOnFinalization(
filePath => new DerivedFileStreamWithoutFinalizer(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, bufferSize: 4096, FileOptions.None));
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported), nameof(PlatformDetection.IsNotBrowser))]
public void FinalizeFlushesWriteBufferForDerivedFileStreamWithoutFinalizerCreatedFromSafeFileHandle()
=> VerifyFlushedBufferOnFinalization(
filePath => new DerivedFileStreamWithoutFinalizer(
File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, FileOptions.None),
FileAccess.Write, bufferSize: 4096, isAsync: false));
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported), nameof(PlatformDetection.IsNotBrowser))]
public void FinalizeFlushesWriteBufferForDerivedFileStreamWithoutFinalizerCreatedFromIntPtr()
=> VerifyFlushedBufferOnFinalization(
filePath => new DerivedFileStreamWithoutFinalizer(
File.OpenHandle(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, FileOptions.None).DangerousGetHandle(),
FileAccess.Write,
ownsHandle: true));
private void VerifyFlushedBufferOnFinalization(Func<string, FileStream> factory)
{
string fileName = GetTestFilePath();
// use a separate method to be sure that fs isn't rooted at time of GC.
Func<string, Type> leakFs = filePath =>
{
// we must specify useAsync:false, otherwise the finalizer just kicks off an async write.
FileStream fs = factory(filePath);
fs.Write(TestBuffer, 0, TestBuffer.Length);
return fs.GetType();
};
int beforeWithFinalizer = DerivedFileStreamWithFinalizer.DisposeFalseCalled;
int beforeWithoutFinalizer = DerivedFileStreamWithoutFinalizer.DisposeFalseCalled;
Type type = leakFs(fileName);
GC.Collect();
GC.WaitForPendingFinalizers();
using (FileStream fsr = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
{
byte[] buffer = new byte[TestBuffer.Length];
Assert.Equal(buffer.Length, fsr.Length);
fsr.Read(buffer, 0, buffer.Length);
Assert.Equal(TestBuffer, buffer);
}
if (type == typeof(DerivedFileStreamWithFinalizer))
{
// derived type finalizer implicitly calls base type finalizer, hence +2
Assert.Equal(beforeWithFinalizer + 2, DerivedFileStreamWithFinalizer.DisposeFalseCalled);
Assert.Equal(0, DerivedFileStreamWithFinalizer.DisposeTrueCalled);
}
else if (type == typeof(DerivedFileStreamWithoutFinalizer))
{
Assert.Equal(beforeWithoutFinalizer + 1, DerivedFileStreamWithoutFinalizer.DisposeFalseCalled);
Assert.Equal(0, DerivedFileStreamWithoutFinalizer.DisposeTrueCalled);
}
}
// this type exists so DerivedFileStreamStrategy can be tested as well
public class DerivedFileStream : FileStream
{
public DerivedFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
: base(path, mode, access, share, bufferSize, options)
{
}
}
public static IEnumerable<object[]> GetFileStreamDisposeSuppressesStrategyFinalizationArgs()
{
foreach (int bufferSize in new[] { 1, 4096 })
{
foreach (FileOptions fileOptions in new[] { FileOptions.Asynchronous, FileOptions.None })
{
yield return new object[] { bufferSize, fileOptions };
}
}
}
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported))]
[MemberData(nameof(GetFileStreamDisposeSuppressesStrategyFinalizationArgs))]
public Task DisposeSuppressesStrategyFinalization(int bufferSize, FileOptions options)
=> VerifyStrategyFinalization(
() => new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, options | FileOptions.DeleteOnClose),
false);
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported))]
[MemberData(nameof(GetFileStreamDisposeSuppressesStrategyFinalizationArgs))]
public Task DisposeSuppressesStrategyFinalizationAsync(int bufferSize, FileOptions options)
=> VerifyStrategyFinalization(
() => new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, options | FileOptions.DeleteOnClose),
true);
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported))]
[MemberData(nameof(GetFileStreamDisposeSuppressesStrategyFinalizationArgs))]
public Task DerivedFileStreamDisposeSuppressesStrategyFinalization(int bufferSize, FileOptions options)
=> VerifyStrategyFinalization(
() => new DerivedFileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, options | FileOptions.DeleteOnClose),
false);
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported))]
[MemberData(nameof(GetFileStreamDisposeSuppressesStrategyFinalizationArgs))]
public Task DerivedFileStreamDisposeSuppressesStrategyFinalizationAsync(int bufferSize, FileOptions options)
=> VerifyStrategyFinalization(
() => new DerivedFileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, options | FileOptions.DeleteOnClose),
true);
private static async Task VerifyStrategyFinalization(Func<FileStream> factory, bool useAsync)
{
WeakReference weakReference = await EnsureFileStreamIsNotRooted(factory, useAsync);
Assert.True(weakReference.IsAlive);
GC.Collect();
Assert.False(weakReference.IsAlive);
// separate method to avoid JIT lifetime-extension issues
static async Task<WeakReference> EnsureFileStreamIsNotRooted(Func<FileStream> factory, bool useAsync)
{
FileStream fs = factory();
WeakReference weakReference = new WeakReference(
(Stream)typeof(FileStream)
.GetField("_strategy", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance)
.GetValue(fs),
trackResurrection: true);
Assert.True(weakReference.IsAlive);
if (useAsync)
{
await fs.DisposeAsync();
}
{
fs.Dispose();
}
return weakReference;
}
}
}
}