microsoftfeedback / WinDbg-Feedback

Public issue and feedback tracking for WinDbg Preview.
Creative Commons Attribution 4.0 International
49 stars 12 forks source link

Please provide a way to close the opened TextReader object #179

Open lowleveldesign opened 7 months ago

lowleveldesign commented 7 months ago

The FileSystem.CreateTextReader method is a great helper when writing WinDbg extensions. Unfortunately, when I create such a reader in JavaScript, the opened file gets locked and even reopening with the same script fails. Example js script:

function initializeScript()
{
    return [new host.apiVersionSupport(1, 7)]
}

function readFirstLine(path) {
    const fileSystem = host.namespace.Debugger.Utility.FileSystem;
    if (!fileSystem.FileExists(path)) {
        throw new Error(`File not found: ${path}`);
    }
    const reader = fileSystem.CreateTextReader(path, "Utf8");
    return reader.ReadLine();
}

And its execution:

0:000> dx @$scriptContents.readFirstLine("C:\\temp\\test.txt")
@$scriptContents.readFirstLine("C:\\temp\\test.txt") : test line sd
    Length           : 0xc
0:000> dx @$scriptContents.readFirstLine("C:\\temp\\test.txt")
Error: Unable to create file [at test-script (line 25 col 5)]

I suspect that JS GC might eventually free it, but it is not deterministic and problematic. Could you please then provide us with a Close (or other) method to free the file handle deterministically?