From beb78b1499a2e7cdf7a07a53216a45e86ff9ee00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20P=C3=A4tzel?= Date: Wed, 23 Jul 2025 10:34:24 +0200 Subject: [PATCH 1/2] Use proper tempfiles in {de,}serialize Serialization was broken/errored due to always using the same fixed file name for the temporary pickle files. This is a problem in setups where multiple instances of XCSF run in parallel (e.g. on Slurm clusters). --- xcsf/pybind_wrapper.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/xcsf/pybind_wrapper.cpp b/xcsf/pybind_wrapper.cpp index aee42bc31..baa0f0b99 100644 --- a/xcsf/pybind_wrapper.cpp +++ b/xcsf/pybind_wrapper.cpp @@ -649,8 +649,15 @@ class XCS py::bytes serialize() const { - // Write XCSF to a temporary binary file - const char *filename = "_tmp_pickle.bin"; + // Create a unique temporary file in /tmp. The file name is made unique + // in place by mkstemp. + char filename[] = "/tmp/xcsf_pickle_XXXXXX"; + int fd = mkstemp(filename); + if (fd == -1) { + throw std::runtime_error("Failed to create temporary file in serialize"); + } + close(fd); + xcsf_save(&xcs, filename); // Read the binary file into bytes std::ifstream file(filename, std::ios::binary); @@ -673,8 +680,16 @@ class XCS static XCS deserialize(const py::bytes &state) { - // Write the XCSF bytes to a temporary binary file - const char *filename = "_tmp_pickle.bin"; + // Create a unique temporary file in /tmp. The file name is made unique + // in place by mkstemp. + char filename[] = "/tmp/xcsf_pickle_XXXXXX"; + int fd = mkstemp(filename); + if (fd == -1) { + throw std::runtime_error("Failed to create temporary file in deserialize"); + } + close(fd); + + // Write the XCSF bytes to the temporary file. std::ofstream file(filename, std::ios::binary); file.write(state.cast().c_str(), state.cast().size()); From c1948050d131562541135b7175348eec9202694d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 8 Aug 2025 09:07:17 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xcsf/pybind_wrapper.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xcsf/pybind_wrapper.cpp b/xcsf/pybind_wrapper.cpp index baa0f0b99..5e781b1f5 100644 --- a/xcsf/pybind_wrapper.cpp +++ b/xcsf/pybind_wrapper.cpp @@ -654,7 +654,8 @@ class XCS char filename[] = "/tmp/xcsf_pickle_XXXXXX"; int fd = mkstemp(filename); if (fd == -1) { - throw std::runtime_error("Failed to create temporary file in serialize"); + throw std::runtime_error( + "Failed to create temporary file in serialize"); } close(fd); @@ -685,7 +686,8 @@ class XCS char filename[] = "/tmp/xcsf_pickle_XXXXXX"; int fd = mkstemp(filename); if (fd == -1) { - throw std::runtime_error("Failed to create temporary file in deserialize"); + throw std::runtime_error( + "Failed to create temporary file in deserialize"); } close(fd);