You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2021/01/27 12:41:28 UTC
[skywalking-nodejs] branch master updated: Use invalid to check
asyncState (#27)
This is an automated email from the ASF dual-hosted git repository.
kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-nodejs.git
The following commit(s) were added to refs/heads/master by this push:
new b2ef178 Use invalid to check asyncState (#27)
b2ef178 is described below
commit b2ef178c3b0498ebb98bd032037d8599c2f71d75
Author: ethan <wx...@gmail.com>
AuthorDate: Wed Jan 27 20:41:19 2021 +0800
Use invalid to check asyncState (#27)
---
src/trace/context/ContextManager.ts | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/src/trace/context/ContextManager.ts b/src/trace/context/ContextManager.ts
index a586856..ef21803 100644
--- a/src/trace/context/ContextManager.ts
+++ b/src/trace/context/ContextManager.ts
@@ -23,7 +23,7 @@ import SpanContext from '../../trace/context/SpanContext';
import async_hooks from 'async_hooks';
-type AsyncState = { context: Context, spans: Span[] };
+type AsyncState = { context: Context, spans: Span[], invalid: boolean };
let store: {
getStore(): AsyncState | undefined;
@@ -59,9 +59,9 @@ if (async_hooks.AsyncLocalStorage) {
class ContextManager {
get asyncState(): AsyncState {
let asyncState = store.getStore();
-
- if (asyncState === undefined) {
- asyncState = { context: new SpanContext(), spans: [] };
+ // since `AsyncLocalStorage.getStore` may get previous state, see issue https://github.com/nodejs/node/issues/35286#issuecomment-697207158, so recreate when asyncState is invalid
+ if (asyncState === undefined || asyncState.invalid) {
+ asyncState = { context: new SpanContext(), spans: [], invalid: false };
store.enterWith(asyncState);
}
@@ -79,10 +79,10 @@ class ContextManager {
spansDup(): Span[] {
let asyncState = store.getStore();
- if (asyncState === undefined) {
- asyncState = { context: new SpanContext(), spans: [] };
+ if (asyncState === undefined || asyncState.invalid) {
+ asyncState = { context: new SpanContext(), spans: [], invalid: false };
} else {
- asyncState = { context: asyncState.context, spans: [...asyncState.spans] };
+ asyncState = { context: asyncState.context, spans: [...asyncState.spans], invalid: asyncState.invalid };
}
store.enterWith(asyncState);
@@ -91,11 +91,12 @@ class ContextManager {
}
clear(): void {
+ this.asyncState.invalid = true;
store.enterWith(undefined as unknown as AsyncState);
}
restore(context: Context, spans: Span[]): void {
- store.enterWith({ context, spans: spans || [] });
+ store.enterWith({ context, spans: spans || [], invalid: this.asyncState.invalid });
}
withSpan(span: Span, callback: (...args: any[]) => any, ...args: any[]): any {