@@ -98,6 +98,7 @@ function makeHang<A>(value: A) {
9898 } ) ;
9999 return {
100100 called,
101+ promise,
101102 hang : ( ) => {
102103 markCalled ( ) ;
103104 return promise ;
@@ -1513,6 +1514,58 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
15131514 } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) , Effect . ensuring ( Effect . sync ( read . release ) ) ) ;
15141515 } ) ,
15151516 ) ;
1517+ it . effect ( "bounds stalled browse reads while callers can still join an existing read" , ( ) =>
1518+ Effect . gen ( function * ( ) {
1519+ const workspaceEntries = yield * WorkspaceEntries . WorkspaceEntries ;
1520+ const firstPath = yield * appendSeparator ( yield * makeTempDir ( ) ) ;
1521+ const secondPath = yield * appendSeparator ( yield * makeTempDir ( ) ) ;
1522+ const availablePath = yield * appendSeparator ( yield * makeTempDir ( ) ) ;
1523+ const first = makeHang < Awaited < ReturnType < typeof NodeFSP . readdir > > > ( [ ] ) ;
1524+ const second = makeHang < Awaited < ReturnType < typeof NodeFSP . readdir > > > ( [ ] ) ;
1525+ const readdir = vi . mocked ( NodeFSP . readdir ) ;
1526+ readdir . mockClear ( ) ;
1527+ readdir
1528+ . mockImplementationOnce ( first . hang as unknown as typeof NodeFSP . readdir )
1529+ . mockImplementationOnce ( second . hang as unknown as typeof NodeFSP . readdir ) ;
1530+ const browse = ( partialPath : string ) => workspaceEntries . browse ( { partialPath } ) ;
1531+
1532+ yield * Effect . gen ( function * ( ) {
1533+ const firstCaller = yield * Effect . forkChild ( browse ( firstPath ) . pipe ( Effect . flip ) ) ;
1534+ yield * Effect . promise ( ( ) => first . called ) ;
1535+ const secondCaller = yield * Effect . forkChild ( browse ( secondPath ) . pipe ( Effect . flip ) ) ;
1536+ yield * Effect . promise ( ( ) => second . called ) ;
1537+ yield * TestClock . adjust ( "5 seconds" ) ;
1538+
1539+ for ( const caller of [ firstCaller , secondCaller ] ) {
1540+ expect ( yield * Fiber . join ( caller ) ) . toMatchObject ( {
1541+ _tag : "WorkspaceEntriesReadDirectoryError" ,
1542+ cause : { reason : { _tag : "TimedOut" } } ,
1543+ } ) ;
1544+ }
1545+ expect ( yield * browse ( availablePath ) . pipe ( Effect . flip ) ) . toMatchObject ( {
1546+ _tag : "WorkspaceEntriesReadDirectoryError" ,
1547+ cause : { reason : { _tag : "TimedOut" } } ,
1548+ } ) ;
1549+
1550+ const joinedCaller = yield * Effect . forkChild ( browse ( firstPath ) ) ;
1551+ yield * TestClock . adjust ( 0 ) ;
1552+ first . release ( ) ;
1553+ expect ( yield * Fiber . join ( joinedCaller ) ) . toMatchObject ( { entries : [ ] } ) ;
1554+ expect ( readdir ) . toHaveBeenCalledTimes ( 2 ) ;
1555+
1556+ expect ( yield * browse ( availablePath ) ) . toMatchObject ( { entries : [ ] } ) ;
1557+ expect ( readdir ) . toHaveBeenCalledTimes ( 3 ) ;
1558+ } ) . pipe (
1559+ Effect . provide ( TestClock . layer ( ) ) ,
1560+ Effect . ensuring (
1561+ Effect . sync ( ( ) => {
1562+ first . release ( ) ;
1563+ second . release ( ) ;
1564+ } ) ,
1565+ ) ,
1566+ ) ;
1567+ } ) ,
1568+ ) ;
15161569 } ) ;
15171570
15181571 describe ( "places" , ( ) => {
@@ -1956,6 +2009,30 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
19562009 } ) ,
19572010 ) ;
19582011
2012+ it . effect ( "closes a folder handle that opens after search interruption" , ( ) =>
2013+ Effect . gen ( function * ( ) {
2014+ const { root, canonicalRoot } = yield * makeTree ( [ ] ) ;
2015+ const actualOpendir = NodeFSP . opendir ;
2016+ const { close } = makeClose ( ) ;
2017+ const read = vi . fn ( ( ) => Promise . resolve ( null ) ) ;
2018+ const opening = makeHang ( { read, close } ) ;
2019+ vi . spyOn ( NodeFSP , "opendir" ) . mockImplementation ( ( ( target , options ) =>
2020+ target === canonicalRoot
2021+ ? opening . hang ( )
2022+ : actualOpendir ( target , options ) ) as typeof NodeFSP . opendir ) ;
2023+
2024+ const search = yield * Effect . forkChild ( searchDirectories ( { rootPath : root , query : "x" } ) ) ;
2025+ yield * Effect . promise ( ( ) => opening . called ) ;
2026+ yield * Fiber . interrupt ( search ) ;
2027+ expect ( close ) . not . toHaveBeenCalled ( ) ;
2028+
2029+ opening . release ( ) ;
2030+ yield * Effect . promise ( ( ) => opening . promise ) ;
2031+ expect ( close ) . toHaveBeenCalledTimes ( 1 ) ;
2032+ expect ( read ) . not . toHaveBeenCalled ( ) ;
2033+ } ) ,
2034+ ) ;
2035+
19592036 it . effect ( "counts root checks against the search deadline" , ( ) =>
19602037 Effect . gen ( function * ( ) {
19612038 const { root, canonicalRoot } = yield * makeTree ( [ "app" ] ) ;
0 commit comments