-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SingleFile bundles: Ensure extraction mappings are closed on Windows. (…
…#2272) When running a single-file app, the AppHost mmap()s itself in order to read its contents and extract the embedded contents. The Apphost must always map its contents in order to read the headers, but doesn't always extract the contents, because previously extracted files are re-used when available. In the case where apphost doesn't extract, the file mapping wasn't immediately closed on Windows. This prevents the app from being renamed while running -- an idiom used while updating the app in-place.
- Loading branch information
1 parent
527adf2
commit b95e523
Showing
8 changed files
with
166 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/installer/test/Assets/TestProjects/AppWithWait/AppWithWait.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NETCoreAppFramework)</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
<RuntimeIdentifier>$(TestTargetRid)</RuntimeIdentifier> | ||
<RuntimeFrameworkVersion>$(MNAVersion)</RuntimeFrameworkVersion> | ||
</PropertyGroup> | ||
|
||
</Project> |
35 changes: 35 additions & 0 deletions
35
src/installer/test/Assets/TestProjects/AppWithWait/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
|
||
namespace AppWithSubDirs | ||
{ | ||
public static class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
Console.Write("Hello "); | ||
|
||
// If the caller wants the app to start and wait, | ||
// it provides the name of a lock-file to write. | ||
// In this case, this test app creates the lock file | ||
// and waits until the file is deleted. | ||
if (args.Length > 0) | ||
{ | ||
string writeFile = args[0]; | ||
|
||
var fs = File.Create(writeFile); | ||
fs.Close(); | ||
|
||
Thread.Sleep(200); | ||
|
||
while (File.Exists(writeFile)) | ||
{ | ||
Thread.Sleep(100); | ||
} | ||
} | ||
|
||
Console.WriteLine("World!"); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/installer/test/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleRename.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.IO; | ||
using Xunit; | ||
using Microsoft.DotNet.Cli.Build.Framework; | ||
using Microsoft.DotNet.CoreSetup.Test; | ||
using BundleTests.Helpers; | ||
using System.Threading; | ||
|
||
namespace AppHost.Bundle.Tests | ||
{ | ||
public class BundleRename : IClassFixture<BundleRename.SharedTestState> | ||
{ | ||
private SharedTestState sharedTestState; | ||
|
||
public BundleRename(SharedTestState fixture) | ||
{ | ||
sharedTestState = fixture; | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] // Test renaming the single-exe during the initial run, when contents are extracted | ||
[InlineData(false)] // Test renaming the single-exe during subsequent runs, when contents are reused | ||
private void Bundle_can_be_renamed_while_running(bool renameFirstRun) | ||
{ | ||
var fixture = sharedTestState.TestFixture.Copy(); | ||
string singleFile = BundleHelper.GetPublishedSingleFilePath(fixture); | ||
string renameFile = Path.Combine(BundleHelper.GetPublishPath(fixture), Path.GetRandomFileName()); | ||
string writeFile = Path.Combine(BundleHelper.GetPublishPath(fixture), "lock"); | ||
|
||
if (!renameFirstRun) | ||
{ | ||
Command.Create(singleFile) | ||
.CaptureStdErr() | ||
.CaptureStdOut() | ||
.Execute() | ||
.Should() | ||
.Pass() | ||
.And | ||
.HaveStdOutContaining("Hello World!"); | ||
} | ||
|
||
var singleExe = Command.Create(singleFile, writeFile) | ||
.CaptureStdErr() | ||
.CaptureStdOut() | ||
.Start(); | ||
|
||
// Once the App starts running, it creates the writeFile, and waits until the file is deleted. | ||
while (!File.Exists(writeFile)) | ||
{ | ||
Thread.Sleep(100); | ||
} | ||
|
||
File.Move(singleFile, renameFile); | ||
File.Delete(writeFile); | ||
|
||
var result = singleExe.WaitForExit(fExpectedToFail: false); | ||
|
||
result | ||
.Should() | ||
.Pass() | ||
.And | ||
.HaveStdOutContaining("Hello World!"); | ||
} | ||
|
||
public class SharedTestState : IDisposable | ||
{ | ||
public TestProjectFixture TestFixture { get; set; } | ||
public RepoDirectoriesProvider RepoDirectories { get; set; } | ||
|
||
public SharedTestState() | ||
{ | ||
RepoDirectories = new RepoDirectoriesProvider(); | ||
TestFixture = new TestProjectFixture("AppWithWait", RepoDirectories); | ||
TestFixture | ||
.EnsureRestoredForRid(TestFixture.CurrentRid, RepoDirectories.CorehostPackages) | ||
.PublishProject(runtime: TestFixture.CurrentRid, | ||
singleFile: true, | ||
outputDirectory: BundleHelper.GetPublishPath(TestFixture)); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
TestFixture.Dispose(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Can't use some class with a destructor?