Skip to content

Commit 0f126fe

Browse files
Try to fix race condition
1 parent 2487424 commit 0f126fe

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

‎neopdf/src/interpolator.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ impl InterpolatorFactory {
412412
pid_index: usize,
413413
) -> Box<dyn DynInterpolator> {
414414
let grid_view = subgrid.grid.view();
415-
// For 3D xi: slice out [xi, x, Q2] from 8D grid [A, alphas, xi, delta, kT, pids, x, Q2]
416415
let grid_data = grid_view
417416
.slice(s![0, 0, .., 0, 0, pid_index, .., ..])
418417
.to_owned();
@@ -453,7 +452,6 @@ impl InterpolatorFactory {
453452
pid_index: usize,
454453
) -> Box<dyn DynInterpolator> {
455454
let grid_view = subgrid.grid.view();
456-
// For 3D delta: slice out [delta, x, Q2] from 8D grid [A, alphas, xi, delta, kT, pids, x, Q2]
457455
let grid_data = grid_view
458456
.slice(s![0, 0, 0, .., 0, pid_index, .., ..])
459457
.to_owned();

‎neopdf/src/writer.rs‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use std::sync::Arc;
2929

3030
use git_version::git_version;
3131
use lz4_flex::frame::{FrameDecoder, FrameEncoder};
32+
use tempfile::NamedTempFile;
3233

3334
use super::gridpdf::GridArray;
3435
use super::metadata::MetaData;
@@ -131,7 +132,12 @@ impl GridArrayCollection {
131132
encoder.write_all(serialized)?;
132133
}
133134

134-
encoder.finish()?;
135+
let mut writer = encoder.finish()?;
136+
writer.flush()?;
137+
138+
// Sync to disk to ensure data is written before returning
139+
writer.get_mut().sync_all()?;
140+
135141
Ok(())
136142
}
137143

@@ -324,13 +330,12 @@ impl GridArrayReader {
324330
new_grids.push(converted_grid);
325331
}
326332

327-
let temp_path =
328-
std::env::temp_dir().join(format!("neopdf_conversion_{}.tmp", std::process::id()));
329-
let grid_refs: Vec<&GridArray> = new_grids.iter().collect();
330-
GridArrayCollection::compress(&grid_refs, &metadata, &temp_path)?;
333+
let temp_file = NamedTempFile::new()?;
334+
let temp_path = temp_file.path();
331335

332-
let result = Self::from_file_v2(&temp_path)?;
333-
std::fs::remove_file(&temp_path).ok();
336+
let grid_refs: Vec<&GridArray> = new_grids.iter().collect();
337+
GridArrayCollection::compress(&grid_refs, &metadata, temp_path)?;
338+
let result = Self::from_file_v2(temp_path)?;
334339

335340
Ok(result)
336341
}
@@ -543,13 +548,12 @@ impl LazyGridArrayIterator {
543548
new_grids.push(converted_grid);
544549
}
545550

546-
let temp_path =
547-
std::env::temp_dir().join(format!("neopdf_lazy_conversion_{}.tmp", std::process::id()));
548-
let grid_refs: Vec<&GridArray> = new_grids.iter().collect();
549-
GridArrayCollection::compress(&grid_refs, &metadata, &temp_path)?;
551+
let temp_file = NamedTempFile::new()?;
552+
let temp_path = temp_file.path();
550553

551-
let result = Self::from_file_v2(&temp_path)?;
552-
std::fs::remove_file(&temp_path).ok();
554+
let grid_refs: Vec<&GridArray> = new_grids.iter().collect();
555+
GridArrayCollection::compress(&grid_refs, &metadata, temp_path)?;
556+
let result = Self::from_file_v2(temp_path)?;
553557

554558
Ok(result)
555559
}

0 commit comments

Comments
 (0)