@@ -7,6 +7,7 @@ use std::sync::Arc;
77
88use crossbeam:: utils:: CachePadded ;
99use fail:: fail_point;
10+ use fs2:: FileExt ;
1011use log:: error;
1112use parking_lot:: { Mutex , MutexGuard , RwLock } ;
1213
@@ -464,7 +465,17 @@ impl<F: FileSystem> SinglePipe<F> {
464465pub struct DualPipes < F : FileSystem > {
465466 pipes : [ SinglePipe < F > ; 2 ] ,
466467
467- _dir_locks : Vec < StdFile > ,
468+ dir_locks : Vec < StdFile > ,
469+ }
470+
471+ impl < F : FileSystem > Drop for DualPipes < F > {
472+ fn drop ( & mut self ) {
473+ for lock in & self . dir_locks {
474+ if let Err ( e) = FileExt :: unlock ( lock) {
475+ error ! ( "error while unlocking directory: {e}" ) ;
476+ }
477+ }
478+ }
468479}
469480
470481impl < F : FileSystem > DualPipes < F > {
@@ -481,7 +492,7 @@ impl<F: FileSystem> DualPipes<F> {
481492
482493 Ok ( Self {
483494 pipes : [ appender, rewriter] ,
484- _dir_locks : dir_locks,
495+ dir_locks,
485496 } )
486497 }
487498
@@ -727,4 +738,21 @@ mod tests {
727738 assert_eq ! ( pipe_log. read_bytes( handle) . unwrap( ) , content( i + 1 ) ) ;
728739 }
729740 }
741+
742+ #[ test]
743+ fn test_release_on_drop ( ) {
744+ let dir = Builder :: new ( )
745+ . prefix ( "test_release_on_drop" )
746+ . tempdir ( )
747+ . unwrap ( ) ;
748+ let path = dir. path ( ) . to_str ( ) . unwrap ( ) ;
749+ let cfg = Config {
750+ dir : path. to_owned ( ) ,
751+ target_file_size : ReadableSize ( 1 ) ,
752+ ..Default :: default ( )
753+ } ;
754+ let pipe_log = new_test_pipes ( & cfg) . unwrap ( ) ;
755+ drop ( pipe_log) ;
756+ assert ! ( new_test_pipes( & cfg) . is_ok( ) ) ;
757+ }
730758}
0 commit comments