{"record":{"id":"56218199c6addeb0","repo":"flxzt/rnote","slug":"finishing-svg-surface-output-stream-failed-err","errorCode":null,"errorMessage":"Finishing Svg surface output stream failed, Err: {e:?}","messagePattern":"Finishing Svg surface output stream failed, Err: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/rnote-engine/src/svg.rs","lineNumber":135,"sourceCode":"                anyhow::anyhow!(\n                    \"Creating svg surface with dimensions ({width}, {height}) failed, Err: {e:?}\"\n                )\n            })?;\n        svg_surface.set_document_unit(cairo::SvgUnit::Px);\n\n        {\n            let cairo_cx = cairo::Context::new(&svg_surface)?;\n            // cairo only draws elements with positive coordinates, so we need to translate the content here\n            cairo_cx.translate(-bounds.mins[0], -bounds.mins[1]);\n            // apply the draw function\n            draw_func(&cairo_cx)?;\n        }\n\n        let content = String::from_utf8(\n            *svg_surface\n                .finish_output_stream()\n                .map_err(|e| {\n                    anyhow::anyhow!(\"Finishing Svg surface output stream failed, Err: {e:?}\")\n                })?\n                .downcast::<Vec<u8>>()\n                .map_err(|e| {\n                    anyhow::anyhow!(\"Downcasting Svg surface content failed, Err: {e:?}\")\n                })?,\n        )?;\n        let svg_data = rnote_compose::utils::remove_xml_header(&content);\n        let mut group = svg::node::element::Group::new().add(svg::node::Blob::new(svg_data));\n        // translate the content back to it's original position\n        group.assign(\n            \"transform\",\n            format!(\"translate({} {})\", bounds.mins[0], bounds.mins[1]),\n        );\n\n        Ok(Self {\n            svg_data: rnote_compose::utils::svg_node_to_string(&group)?,\n            bounds,\n        })","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/flxzt/rnote/blob/bbc5354502ba2fc83eec2670b535348825e679a6/crates/rnote-engine/src/svg.rs#L117-L153","documentation":"Thrown by Svg::gen_with_cairo when cairo's SvgSurface::finish_output_stream() returns a cairo::Error. After all drawing on the SVG surface is done, cairo must flush and finish the backing stream; if the surface is already in an error state (e.g. from a failed earlier draw operation) or was finished twice, this call fails.","triggerScenarios":"Calling finish_output_stream() on an SvgSurface whose context recorded an error during draw_func, calling it twice on the same surface, or cairo marking the surface invalid (invalid dimensions, write failure to the backing stream).","commonSituations":"Drawing with invalid cairo state (e.g. negative/zero surface dimensions slipped through), double-finishing a surface reused for export, or memory/stream failures during large SVG exports in rnote.","solutions":["Check that draw_func did not leave the cairo context in an error state before finishing the stream","Ensure finish_output_stream() is only called once per surface","Validate that width/height passed to SvgSurface::for_stream are positive and finite","Log the wrapped cairo::Error to identify the underlying cairo status"],"exampleFix":"// before\nlet content = String::from_utf8(*svg_surface.finish_output_stream().map_err(|e| anyhow::anyhow!(\"Finishing Svg surface output stream failed, Err: {e:?}\"))?.downcast::<Vec<u8>>()?)?;\n// after\nlet stream = svg_surface.finish_output_stream().map_err(|e| anyhow::anyhow!(\"Finishing Svg surface output stream failed, Err: {e:?}\"))?;\nlet bytes = *stream.downcast::<Vec<u8>>().map_err(|e| anyhow::anyhow!(\"Downcasting failed: {e:?}\"))?;\nlet content = String::from_utf8(bytes)?;","handlingStrategy":"try-catch","validationCode":"debug_assert!(bounds.extents()[0] > 0.0 && bounds.extents()[1] > 0.0);","typeGuard":"fn surface_healthy(s: &cairo::Surface) -> bool { s.status() == cairo::Status::Success }","tryCatchPattern":"match svg_surface.finish_output_stream() {\n    Ok(stream) => { /* downcast and use bytes */ }\n    Err(e) => log::error!(\"svg stream finish failed: {e:?}\"),\n}","preventionTips":["Only finish each surface once","Validate drawing bounds are positive and finite before creating surfaces","Check cairo context status after draw operations in debug builds"],"tags":["cairo","svg","rendering"],"backgroundTag":"http-request-failed","analyzedSha":"bbc5354502ba2fc83eec2670b535348825e679a6","analyzedAt":"2026-09-08T13:20:33.747Z","contentChangedAt":"2026-09-08T13:20:33.747Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}