@@ -48,7 +48,7 @@ struct AsyncState {
48
48
latest_version : u64 ,
49
49
50
50
// The last version that was written to disk.
51
- last_written_version : u64 ,
51
+ latest_written_version : u64 ,
52
52
}
53
53
54
54
struct FilesystemStoreInner {
@@ -201,17 +201,28 @@ impl FilesystemStoreInner {
201
201
panic ! ( "FilesystemStore version counter overflowed" ) ;
202
202
}
203
203
204
+ debug_assert ! ( async_state. latest_version > async_state. latest_written_version) ;
205
+
204
206
async_state. latest_version
205
207
}
206
208
207
209
fn read ( & self , dest_file_path : PathBuf ) -> lightning:: io:: Result < Vec < u8 > > {
208
210
let mut buf = Vec :: new ( ) ;
209
211
{
210
212
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 ( ) ;
212
214
213
- let mut f = fs:: File :: open ( dest_file_path) ?;
215
+ let mut f = fs:: File :: open ( dest_file_path. clone ( ) ) ?;
214
216
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
+ }
215
226
}
216
227
217
228
Ok ( buf)
@@ -228,18 +239,18 @@ impl FilesystemStoreInner {
228
239
229
240
// Check if we already have a newer version written/removed. This is used in async contexts to realize eventual
230
241
// consistency.
231
- let stale = version <= async_state. last_written_version ;
242
+ let is_stale_version = version <= async_state. latest_written_version ;
232
243
233
244
// 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 {
235
246
Ok ( ( ) )
236
247
} else {
237
248
callback ( ) . map ( |_| {
238
- async_state. last_written_version = version;
249
+ async_state. latest_written_version = version;
239
250
} )
240
251
} ;
241
252
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 ;
243
254
244
255
// If there are no more writes pending and no arcs in use elsewhere, we can remove the map entry to prevent
245
256
// 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 {
381
392
382
393
call ! ( unsafe {
383
394
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
387
398
| windows_sys:: Win32 :: Storage :: FileSystem :: MOVEFILE_REPLACE_EXISTING ,
388
- )
399
+ )
389
400
} ) ?;
390
401
391
402
{
0 commit comments