Skip to content

Commit 528b414

Browse files
committed
f: various fixes
1 parent 6175bd4 commit 528b414

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct AsyncState {
4848
latest_version: u64,
4949

5050
// The last version that was written to disk.
51-
last_written_version: u64,
51+
latest_written_version: u64,
5252
}
5353

5454
struct FilesystemStoreInner {
@@ -201,17 +201,28 @@ impl FilesystemStoreInner {
201201
panic!("FilesystemStore version counter overflowed");
202202
}
203203

204+
debug_assert!(async_state.latest_version > async_state.latest_written_version);
205+
204206
async_state.latest_version
205207
}
206208

207209
fn read(&self, dest_file_path: PathBuf) -> lightning::io::Result<Vec<u8>> {
208210
let mut buf = Vec::new();
209211
{
210212
let inner_lock_ref = self.get_inner_lock_ref(dest_file_path.clone());
211-
let _guard = inner_lock_ref.read().unwrap();
213+
let async_state = inner_lock_ref.read().unwrap();
212214

213-
let mut f = fs::File::open(dest_file_path)?;
215+
let mut f = fs::File::open(dest_file_path.clone())?;
214216
f.read_to_end(&mut buf)?;
217+
218+
let more_writes_pending =
219+
async_state.latest_written_version < async_state.latest_version;
220+
221+
// If there are no more writes pending and no arcs in use elsewhere, we can remove the map entry to prevent
222+
// leaking memory. The two arcs are the one in the map and the one held here in inner_lock_ref.
223+
if !more_writes_pending && Arc::strong_count(&inner_lock_ref) == 2 {
224+
self.locks.lock().unwrap().remove(&dest_file_path);
225+
}
215226
}
216227

217228
Ok(buf)
@@ -228,18 +239,18 @@ impl FilesystemStoreInner {
228239

229240
// Check if we already have a newer version written/removed. This is used in async contexts to realize eventual
230241
// consistency.
231-
let stale = version <= async_state.last_written_version;
242+
let is_stale_version = version <= async_state.latest_written_version;
232243

233244
// If the version is not stale, we execute the callback. Otherwise we can and must skip writing.
234-
let res = if stale {
245+
let res = if is_stale_version {
235246
Ok(())
236247
} else {
237248
callback().map(|_| {
238-
async_state.last_written_version = version;
249+
async_state.latest_written_version = version;
239250
})
240251
};
241252

242-
let more_writes_pending = async_state.last_written_version < async_state.latest_version;
253+
let more_writes_pending = async_state.latest_written_version < async_state.latest_version;
243254

244255
// If there are no more writes pending and no arcs in use elsewhere, we can remove the map entry to prevent
245256
// leaking memory. The two arcs are the one in the map and the one held here in inner_lock_ref.
@@ -381,11 +392,11 @@ impl FilesystemStoreInner {
381392

382393
call!(unsafe {
383394
windows_sys::Win32::Storage::FileSystem::MoveFileExW(
384-
path_to_windows_str(&dest_file_path).as_ptr(),
385-
path_to_windows_str(&trash_file_path).as_ptr(),
386-
windows_sys::Win32::Storage::FileSystem::MOVEFILE_WRITE_THROUGH
395+
path_to_windows_str(&dest_file_path).as_ptr(),
396+
path_to_windows_str(&trash_file_path).as_ptr(),
397+
windows_sys::Win32::Storage::FileSystem::MOVEFILE_WRITE_THROUGH
387398
| windows_sys::Win32::Storage::FileSystem::MOVEFILE_REPLACE_EXISTING,
388-
)
399+
)
389400
})?;
390401

391402
{

0 commit comments

Comments
 (0)