diff --git a/src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs b/src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs index 2f17b18265ec230ba6aa12cf8221746a2906eff8..d66204a52aec25add63cd16c77fdc499b3571cde 100644 --- a/src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs +++ b/src/Compilers/CSharp/Test/Emit/Emit/CompilationEmitTests.cs @@ -2976,5 +2976,24 @@ public static void Main() // error CS8104: An error occurred while writing the Portable Executable file. Diagnostic(ErrorCode.ERR_PeWritingFailure).WithArguments("I/O error occurred.").WithLocation(1, 1)); } + + [Fact] + [WorkItem(9308, "https://github.com/dotnet/roslyn/issues/9308")] + public void FailingEmitterAllowsCancelationExceptionsThrough() + { + string source = @" +public class X +{ + public static void Main() + { + + } +}"; + var compilation = CreateCompilationWithMscorlib(source); + var broken = new BrokenStream(); + broken.BreakHow = 3; + + Assert.Throws(() => compilation.Emit(broken)); + } } } diff --git a/src/Compilers/Core/Portable/PEWriter/PeWriter.cs b/src/Compilers/Core/Portable/PEWriter/PeWriter.cs index a42fb3c599bf8e58310b62f232ce4e4d1e859f09..8e5b74e74ec78ce3b4cc8d215a1483c249fedd64 100644 --- a/src/Compilers/Core/Portable/PEWriter/PeWriter.cs +++ b/src/Compilers/Core/Portable/PEWriter/PeWriter.cs @@ -96,7 +96,7 @@ internal sealed class PeWriter return peWriter.WritePeToStream(mdWriter, getPeStream, getPortablePdbStreamOpt, nativePdbWriterOpt); } - catch (Exception ex) when (!(ex is PdbWritingException || ex is ResourceException || ex is PermissionSetFileReadException)) + catch (Exception ex) when (!(ex is PdbWritingException || ex is ResourceException || ex is PermissionSetFileReadException || ex is OperationCanceledException)) { throw new PeWritingException(ex); } diff --git a/src/Test/Utilities/Shared/Pe/BrokenStream.cs b/src/Test/Utilities/Shared/Pe/BrokenStream.cs index 24c000eff7fcc62e5abac943b340f8ca8da74edd..44ea2d4211caba3f9f24ae7a75a10ee2a0c9af68 100644 --- a/src/Test/Utilities/Shared/Pe/BrokenStream.cs +++ b/src/Test/Utilities/Shared/Pe/BrokenStream.cs @@ -68,7 +68,13 @@ public override void SetLength(long value) public override void Write(byte[] buffer, int offset, int count) { if (BreakHow == 0) + { throw new IOException(); + } + if (BreakHow == 3) + { + throw new OperationCanceledException(); + } } } }