Skip to content

Commit e71e990

Browse files
committed
fix(context): close invocation context only after async is done
1 parent 8a8857d commit e71e990

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

packages/context/src/__tests__/unit/invocation-context.unit.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ describe('InvocationContext', () => {
6060
expect(invocationCtxForCheckName.invokeTargetMethod()).to.eql(true);
6161
});
6262

63+
it('does not close when an interceptor is in processing', () => {
64+
const result = invocationCtxForGreet.invokeTargetMethod();
65+
expect(invocationCtxForGreet.isBound('abc'));
66+
return result;
67+
});
68+
6369
class MyController {
6470
static checkName(name: string) {
6571
const firstLetter = name.substring(0, 1);
@@ -73,6 +79,7 @@ describe('InvocationContext', () => {
7379

7480
function givenContext() {
7581
ctx = new Context();
82+
ctx.bind('abc').to('xyz');
7683
}
7784

7885
function givenInvocationContext() {

packages/context/src/interceptor.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {filterByTag} from './binding-filter';
1818
import {BindingAddress} from './binding-key';
1919
import {Context} from './context';
2020
import {ContextBindings, ContextTags} from './keys';
21-
import {transformValueOrPromise, ValueOrPromise} from './value-promise';
21+
import {
22+
transformValueOrPromise,
23+
tryWithFinally,
24+
ValueOrPromise,
25+
} from './value-promise';
2226
const debug = debugFactory('loopback:context:interceptor');
2327
const getTargetName = DecoratorFactory.getTargetName;
2428

@@ -390,12 +394,13 @@ export function invokeMethodWithInterceptors(
390394
);
391395

392396
invocationCtx.assertMethodExists();
393-
try {
394-
const interceptors = invocationCtx.loadInterceptors();
395-
return invokeInterceptors(invocationCtx, interceptors);
396-
} finally {
397-
invocationCtx.close();
398-
}
397+
return tryWithFinally(
398+
() => {
399+
const interceptors = invocationCtx.loadInterceptors();
400+
return invokeInterceptors(invocationCtx, interceptors);
401+
},
402+
() => invocationCtx.close(),
403+
);
399404
}
400405

401406
/**

0 commit comments

Comments
 (0)