@@ -9,7 +9,10 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
99import { HostProcessEnvironment } from "@t3tools/shared/hostProcess" ;
1010import { UsageDay , type UsageSummaryInput } from "@t3tools/contracts" ;
1111import * as Effect from "effect/Effect" ;
12+ import * as Exit from "effect/Exit" ;
13+ import * as Fiber from "effect/Fiber" ;
1214import * as Layer from "effect/Layer" ;
15+ import * as Scheduler from "effect/Scheduler" ;
1316import { HttpClient , HttpClientResponse } from "effect/unstable/http" ;
1417
1518import * as ServerConfig from "../config.ts" ;
@@ -138,4 +141,86 @@ describe("UsageService", () => {
138141 assert . strictEqual ( ratesFetches , 2 ) ;
139142 } ) . pipe ( Effect . scoped ) ,
140143 ) ;
144+
145+ it . live ( "does not orphan an in-flight scan when its first caller is interrupted" , ( ) =>
146+ Effect . gen ( function * ( ) {
147+ const { settings, home } = yield * setup ;
148+ const service = yield * UsageService . make . pipe (
149+ Effect . provide (
150+ serviceLayers ( { prefix : "usage-service-interruption-test" , home, settings } ) ,
151+ ) ,
152+ ) ;
153+
154+ let orphanedAt : number | undefined ;
155+ for ( let interruptAt = 1 ; interruptAt <= 31 ; interruptAt += 1 ) {
156+ const tasks : Array < ( ) => void > = [ ] ;
157+ const dispatcher : Scheduler . SchedulerDispatcher = {
158+ scheduleTask : ( task ) => tasks . push ( task ) ,
159+ flush : ( ) => {
160+ let task : ( ( ) => void ) | undefined ;
161+ while ( ( task = tasks . shift ( ) ) !== undefined ) task ( ) ;
162+ } ,
163+ } ;
164+
165+ let requestFiber : Fiber . Fiber < unknown , unknown > | undefined ;
166+ let requestChecks = 0 ;
167+ const scheduler : Scheduler . Scheduler = {
168+ executionMode : "async" ,
169+ makeDispatcher : ( ) => dispatcher ,
170+ shouldYield : ( fiber ) => {
171+ if ( fiber !== requestFiber ) return false ;
172+ requestChecks += 1 ;
173+ if ( requestChecks !== interruptAt ) return false ;
174+ fiber . interruptUnsafe ( ) ;
175+ return true ;
176+ } ,
177+ } ;
178+
179+ // Each candidate needs a distinct key because the broken case leaves
180+ // its entry in the service's private in-flight map. The invalid window
181+ // keeps the real scan synchronous once its detached fiber starts.
182+ const input : UsageSummaryInput = {
183+ ...WINDOW ,
184+ sinceDay : UsageDay . make ( "2026-09-01" ) ,
185+ untilDay : UsageDay . make ( `2026-08-${ String ( interruptAt ) . padStart ( 2 , "0" ) } ` ) ,
186+ } ;
187+ const first = yield * service
188+ . readSummary ( input )
189+ . pipe (
190+ Effect . exit ,
191+ Effect . provideService ( Scheduler . Scheduler , scheduler ) ,
192+ Effect . forkChild ,
193+ ) ;
194+ requestFiber = first ;
195+ yield * Effect . yieldNow ;
196+ dispatcher . flush ( ) ;
197+
198+ const second = yield * service . readSummary ( input ) . pipe (
199+ Effect . match ( {
200+ onFailure : ( error ) => error . reason ,
201+ onSuccess : ( ) => "success" as const ,
202+ } ) ,
203+ Effect . provideService ( Scheduler . Scheduler , scheduler ) ,
204+ Effect . forkChild ,
205+ ) ;
206+ yield * Effect . yieldNow ;
207+ dispatcher . flush ( ) ;
208+ const secondExit = second . pollUnsafe ( ) ;
209+ if ( secondExit === undefined ) {
210+ second . interruptUnsafe ( ) ;
211+ orphanedAt = interruptAt ;
212+ break ;
213+ }
214+ if ( Exit . isFailure ( secondExit ) ) {
215+ assert . fail ( "the matching request fiber was interrupted" ) ;
216+ }
217+ assert . strictEqual ( secondExit . value , "invalidWindow" ) ;
218+ }
219+
220+ assert . isUndefined (
221+ orphanedAt ,
222+ `interruption left the next matching request pending at scheduler check ${ orphanedAt } ` ,
223+ ) ;
224+ } ) . pipe ( Effect . scoped ) ,
225+ ) ;
141226} ) ;
0 commit comments