Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOException in ZipArchiveEntry.OpenInUpdateMode when printing #9418

Open
bgrainger opened this issue Jul 16, 2024 · 4 comments
Open

IOException in ZipArchiveEntry.OpenInUpdateMode when printing #9418

bgrainger opened this issue Jul 16, 2024 · 4 comments
Labels
🚧 work in progress Investigate Requires further investigation by the WPF team.

Comments

@bgrainger
Copy link
Contributor

bgrainger commented Jul 16, 2024

Description

This is a repost of #6842 with a repro.

In a WPF application ported to .NET 8.0 from .NET Framework 4.7.2, printing a complex document throws an exception, and nothing prints. (The Microsoft Print to PDF driver writes a 0-byte file.)

Reproduction Steps

Clone https://github.com/bgrainger/Bug6842. Build and run the app. Press the Print button. (If your default printer is "Print to PDF" or similar, specify a file name to print to when the Save dialog appears.) The application crashes.

The minimal code to repro is:

// Create a FlowDocument with an embedded image
FlowDocument flowDocument = new FlowDocument
{
    Blocks =
    {
        new Paragraph
        {
            // ** this line triggers the exception below **
            Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/sample.bmp"))),

            Inlines =
            {
                new Run { Text = "test" },
            },
        },
    }
};
            
// create a temp XPS document
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
var tempDocument = new XpsDocument(tempFilePath, FileAccess.ReadWrite);
var writer = XpsDocument.CreateXpsDocumentWriter(tempDocument);

// Write the FlowDocument to the XPS document
writer.Write(((IDocumentPaginatorSource) flowDocument).DocumentPaginator);

// Get a default print ticket from the default printer
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueue printQueue = LocalPrintServer.GetDefaultPrintQueue();
PrintTicket printTicket = printQueue.DefaultPrintTicket;

// Create an XpsDocumentWriter object for the print queue.
XpsDocumentWriter printWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);

// ** this throws the exception **
printWriter.Write(tempDocument.GetFixedDocumentSequence(), printTicket);

// avoiding the temporary document prints successfully
// xpsDocumentWriter.Write(((IDocumentPaginatorSource) flowDocument).DocumentPaginator, printTicket);

Expected behavior

The FlowDocument (with an embedded image) is printed to the selected printer.

Actual behavior

The application crashes and nothing is printed.

System.IO.Compression.dll!System.IO.Compression.ZipArchiveEntry.OpenInUpdateMode() Line 714	C#	Symbols loaded.
System.IO.Compression.dll!System.IO.Compression.ZipArchiveEntry.Open() Line 342	C#	Symbols loaded.
System.IO.Packaging.dll!System.IO.Packaging.ZipStreamManager.Open(System.IO.Compression.ZipArchiveEntry zipArchiveEntry, System.IO.FileAccess streamFileAccess) Line 78	C#	Symbols loaded.
System.IO.Packaging.dll!System.IO.Packaging.ZipPackagePart.GetStreamCore(System.IO.FileMode streamFileMode, System.IO.FileAccess streamFileAccess) Line 28	C#	Symbols loaded.
System.IO.Packaging.dll!System.IO.Packaging.PackagePart.GetStream(System.IO.FileMode mode, System.IO.FileAccess access) Line 154	C#	Symbols loaded.
WindowsBase.dll!MS.Internal.IO.Packaging.PackagePartExtensions.GetSeekableStream(System.IO.Packaging.PackagePart packPart, System.IO.FileMode mode, System.IO.FileAccess access) Line 62	C#	Symbols loaded.
PresentationCore.dll!System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream() Line 652	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ImageSourceTypeConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) Line 245	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.VisualSerializer.WriteBitmap(string attribute, System.Windows.Media.ImageSource imageSource) Line 647	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.VisualSerializer.BrushToString(System.Windows.Media.Brush brush, System.Windows.Rect bounds) Line 772	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.VisualSerializer.FindBrush(System.Windows.Media.Brush brush, System.Windows.Rect bounds) Line 273	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.VisualSerializer.WriteBrush(string attribute, System.Windows.Media.Brush brush, System.Windows.Rect bounds) Line 903	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.VisualSerializer.System.Windows.Xps.Serialization.IMetroDrawingContext.DrawGeometry(System.Windows.Media.Brush brush, System.Windows.Media.Pen pen, System.Windows.Media.Geometry geometry) Line 1923	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.DrawingContextFlattener.DrawGeometry(System.Windows.Media.Brush brush, System.Windows.Media.Pen pen, System.Windows.Media.Geometry geometry) Line 281	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.VisualTreeFlattener.DrawingWalk(System.Windows.Media.Drawing d, System.Windows.Media.Matrix drawingToWorldTransform) Line 697	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.VisualTreeFlattener.DrawingWalk(System.Windows.Media.Drawing d, System.Windows.Media.Matrix drawingToWorldTransform) Line 763	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.VisualTreeFlattener.StartVisual(System.Windows.Media.Visual visual) Line 638	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachVisualSerializer.SerializeTree(System.Windows.Media.Visual visual, System.Xml.XmlWriter resWriter, System.Xml.XmlWriter bodyWriter) Line 111	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachUIElementCollectionSerializer.SerializeUIElements(System.Windows.Xps.Serialization.SerializableObjectContext serializableObjectContext) Line 138	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(System.Windows.Xps.Serialization.SerializablePropertyContext serializedProperty) Line 176	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeProperties(System.Windows.Xps.Serialization.SerializableObjectContext serializableObjectContext) Line 341	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.XpsOMFixedPageSerializer.PersistObjectData(System.Windows.Xps.Serialization.SerializableObjectContext serializableObjectContext) Line 114	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(object serializedObject) Line 112	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(object serializedObject) Line 112	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachPageContentCollectionSerializer.SerializePageContent(object pageContent) Line 140	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachPageContentCollectionSerializer.SerializePageContents(System.Windows.Xps.Serialization.SerializableObjectContext serializableObjectContext) Line 116	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(System.Windows.Xps.Serialization.SerializablePropertyContext serializedProperty) Line 176	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeProperties(System.Windows.Xps.Serialization.SerializableObjectContext serializableObjectContext) Line 341	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.XpsOMFixedDocumentSerializer.PersistObjectData(System.Windows.Xps.Serialization.SerializableObjectContext serializableObjectContext) Line 106	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(object serializedObject) Line 112	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(object serializedObject) Line 112	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachDocumentReferenceCollectionSerializer.SerializeDocumentReferences(System.Windows.Xps.Serialization.SerializableObjectContext serializableObjectContext) Line 86	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(System.Windows.Xps.Serialization.SerializablePropertyContext serializedProperty) Line 176	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeProperties(System.Windows.Xps.Serialization.SerializableObjectContext serializableObjectContext) Line 341	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.XpsOMDocumentSequenceSerializer.PersistObjectData(System.Windows.Xps.Serialization.SerializableObjectContext serializableObjectContext) Line 52	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(object serializedObject) Line 112	C#	Symbols loaded.
ReachFramework.dll!System.Windows.Xps.Serialization.XpsOMSerializationManager.SaveAsXaml(object serializedObject) Line 126	C#	Symbols loaded.
System.Printing.dll!System::Windows::Xps::XpsDocumentWriter::SaveAsXaml(System::Object^ serializedObject, bool isSync) Line 2291	C++	Symbols loaded.
Bug6842.dll!Bug6842.MainWindow.PrintButton_Click(object sender, System.Windows.RoutedEventArgs e) Line 69	C#	Symbols loaded.
PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Line 206	C#	Symbols loaded.
PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) Line 2340	C#	Symbols loaded.
PresentationFramework.dll!System.Windows.Controls.Primitives.ButtonBase.OnClick() Line 72	C#	Symbols loaded.
PresentationFramework.dll!System.Windows.Controls.Button.OnClick() Line 273	C#	Symbols loaded.
PresentationFramework.dll!System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e) Line 504	C#	Symbols loaded.
PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Line 335	C#	Symbols loaded.
PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Line 206	C#	Symbols loaded.
PresentationCore.dll!System.Windows.UIElement.ReRaiseEventAs(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args, System.Windows.RoutedEvent newEvent) Line 2286	C#	Symbols loaded.
PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Line 335	C#	Symbols loaded.
PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Line 206	C#	Symbols loaded.
PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) Line 2340	C#	Symbols loaded.
PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args) Line 457	C#	Symbols loaded.
PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() Line 805	C#	Symbols loaded.
PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport) Line 105	C#	Symbols loaded.
PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(nint hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawMouseActions actions, int x, int y, int wheel) Line 1438	C#	Symbols loaded.
PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(nint hwnd, MS.Internal.Interop.WindowMessage msg, nint wParam, nint lParam, ref bool handled) Line 527	C#	Symbols loaded.
PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled) Line 1592	C#	Symbols loaded.
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled) Line 299	C#	Symbols loaded.
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Line 103	C#	Symbols loaded.
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Line 36	C#	Symbols loaded.
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Line 1339	C#	Symbols loaded.
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(nint hwnd, int msg, nint wParam, nint lParam) Line 341	C#	Symbols loaded.
[Native to Managed Transition]		Annotated Frame
[Managed to Native Transition]		Annotated Frame
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) Line 2143	C#	Symbols loaded.
PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore) Line 2406	C#	Symbols loaded.
PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window) Line 1693	C#	Symbols loaded.
Bug6842.dll!Bug6842.App.Main()	Unknown	Symbols loaded.

Regression?

This is a regression from .NET Framework 4.7.2 and affects my company's product that was ported from that version.

It was present in .NET 6.0 (see #6842).

Known Workarounds

Yes; see comment: #9418 (comment).

Impact

Our application can't print on Windows when running on .NET 8.0.

Configuration

dotnet --info
.NET SDK:
 Version:           8.0.303
 Commit:            29ab8e3268
 Workload version:  8.0.300-manifests.34944930
 MSBuild version:   17.10.4+10fbfbf2e

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19045
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.303\

Other information

A possible fix is in this PR: #6843.

@bgrainger
Copy link
Contributor Author

This is a regression from .NET Framework 4.7.2

I had to update the sample app slightly to work correctly under .NET 4.7.2 (but the primary point still stands). The updated code runs under net472 but still crashes under net8.0-windows.

Replace the two lines to create the XpsDocument with:

// create a temporary package
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Package package = Package.Open(tempFilePath, FileMode.Create, FileAccess.ReadWrite);

// create a temporary XPS document using that package
string tempFilePath2 = Path.GetTempFileName();
PackageStore.AddPackage(new Uri(tempFilePath2), package);
var tempDocument = new XpsDocument(package, CompressionOption.Normal, tempFilePath2);

See bgrainger/Bug6842@409cf4c.

@hongruiyu
Copy link

Suggestion:

If you could write directly to FlowDocument, there is no need to nest another layer of XpsDocument. In this case, just use printWriter.Write(((IDocumentPaginatorSource) flowDocument).DocumentPaginator, printTicket);

Analysis:
According to the error message, the XpsDocument is opened multiple times.
writer.Write(((IDocumentPaginatorSource)flowDocument).DocumentPaginator); and
printWriter.Write(tempDocument.GetFixedDocumentSequence(), printTicket); both need to open the XpsDocument for reading and writing.

I tried using Close() to close the first opening, but it didn't seem to work.

In the official sample program (https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/documents/printing-overview?view=netdesktop-8.0#xpsdocumentwriter), it also seems to avoid opening the XpsDocument file multiple times for reading and writing.

@bgrainger
Copy link
Contributor Author

The FlowDocument is a simplification for this repro; it's not used by the application where this problem was first discovered (which uses a custom DocumentPaginator and draws directly to a Visual, then saves everything to an XpsDocument so it can show a "print preview" on screen, then prints that XpsDocument).

@bgrainger
Copy link
Contributor Author

The exception is coming from OpenInUpdateMode. This seemed odd to me, because we should just be "reading" the document to print it, not updating it.

ZipPackage sets the ZipArchiveMode to Update if it's opened with FileAccess.ReadWrite: https://github.com/dotnet/runtime/blob/31bc167803abe8381c6ecdfec1f7935dead2df10/src/libraries/System.IO.Packaging/src/System/IO/Packaging/ZipPackage.cs#L395-L396

That value comes from Package.Open: https://github.com/dotnet/runtime/blob/31bc167803abe8381c6ecdfec1f7935dead2df10/src/libraries/System.IO.Packaging/src/System/IO/Packaging/Package.cs#L854

That value comes from the XpsManager constructor:

if( packageAccess == FileAccess.Write )
{
// In .NET Core 3.0 System.IO.Compression's ZipArchive does not allow creation of ZipArchiveEntries when
// a prior ZipArchiveEntry is still open. XPS Serialization requires this as part of its implementation.
// To get around this, XPS creation should occur in with FileAccess.ReadWrite. This allows multiple
// ZipArchiveEntries to be open concurrently.
package = Package.Open(path,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.None);
streaming = true;
}
else
{
package = Package.Open(path,
(packageAccess== FileAccess.Read) ? FileMode.Open: FileMode.OpenOrCreate,
packageAccess,
(packageAccess== FileAccess.Read) ? FileShare.Read: FileShare.None
);
}

Which comes from the XpsDocument constructor:

public
XpsDocument(
string path,
FileAccess packageAccess,
CompressionOption compressionOption
)
: base(new XpsManager( path,
packageAccess,
compressionOption
)
)

Thus, it seems like the bug may be getting triggered from printing an XPS Document that is still open for writing. I updated the code to close and reopen the document. This allows it to be printed successfully: bgrainger/Bug6842@4d5eabc.

I have applied this fix to our complex WPF application and it resolves the bug.

I believe this is still a regression from .NET Framework (and perhaps could manifest in other code paths?) but it is now less serious for us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚧 work in progress Investigate Requires further investigation by the WPF team.
Projects
None yet
Development

No branches or pull requests

3 participants