Skip to content

Commit 4266c27

Browse files
authored
fix #594 memory leaks caused by cls-hooked (#595)
* fix #594 memory leaks caused by cls-hooked * fix #594 memory leaks caused by cls-hooked * fix #594 memory leaks caused by cls-hooked * fix #594 memory leaks caused by cls-hooked
1 parent 5eb97ef commit 4266c27

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

packages/core/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ mechanisms, but a few are supplied. See Capturing Function Calls below.
2222
The AWS X-Ray SDK has two modes: `manual` and `automatic`.
2323
By default, the SDK is in automatic mode. You can flip the mode of the SDK using the following:
2424

25-
AWSXRay.enableManualMode();
25+
```js
26+
AWSXRay.enableAutomaticMode();
27+
28+
AWSXRay.enableManualMode();
2629

27-
AWSXRay.enableAutomaticMode();
30+
/* see https://github.com/aws/aws-xray-sdk-node/pull/595
31+
for details on using this environment variable
32+
to prevent memory leaks when using manual mode
33+
*/
34+
process.env.AWS_XRAY_MANUAL_MODE = 'true';
35+
```
2836

2937
#### Automatic mode
3038

@@ -54,6 +62,7 @@ section for different usages.
5462
**Environment variables always override values set in code.**
5563

5664
AWS_XRAY_DEBUG_MODE Enables logging of debug messages to console output. Logging to a file is no longer built in. See 'configure logging' below.
65+
AWS_XRAY_MANUAL_MODE For overriding the default automatic mode. See 'Automatic mode'.
5766
AWS_XRAY_TRACING_NAME For overriding the default segment name to use
5867
with the middleware. See 'dynamic and fixed naming modes'.
5968
AWS_XRAY_DAEMON_ADDRESS For setting the daemon address and port.

packages/core/lib/context_utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,13 @@ var contextUtils = {
205205
}
206206
};
207207

208-
cls.createNamespace(NAMESPACE);
209-
logger.getLogger().debug('Starting the AWS X-Ray SDK in automatic mode (default).');
208+
if (process.env.AWS_XRAY_MANUAL_MODE) {
209+
cls_mode = false;
210+
logger.getLogger().debug('Starting the AWS X-Ray SDK in manual mode.');
211+
} else {
212+
cls.createNamespace(NAMESPACE);
213+
logger.getLogger().debug('Starting the AWS X-Ray SDK in automatic mode (default).');
214+
}
210215

211216
if (process.env.AWS_XRAY_CONTEXT_MISSING) {
212217
contextUtils.setContextMissingStrategy(process.env.AWS_XRAY_CONTEXT_MISSING);

packages/core/test/unit/context_utils.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var RUNTIME_ERROR = 'RUNTIME_ERROR';
1111
var RUNTIME_ERROR_FCN_NAME = 'contextMissingRuntimeError';
1212
var IGNORE_ERROR = 'IGNORE_ERROR';
1313
var IGNORE_ERROR_FCN_NAME = 'contextMissingIgnoreError';
14+
var cls = require('cls-hooked/context');
1415

1516
describe('ContextUtils', function() {
1617
function reloadContextUtils() {
@@ -30,11 +31,20 @@ describe('ContextUtils', function() {
3031
afterEach(function() {
3132
sandbox.restore();
3233
delete process.env.AWS_XRAY_CONTEXT_MISSING;
34+
delete process.env.AWS_XRAY_MANUAL_MODE;
35+
cls.reset();
3336
reloadContextUtils();
3437
});
3538

39+
it('should start in manual mode when process.env.AWS_XRAY_MANUAL_MODE is present', function() {
40+
process.env.AWS_XRAY_MANUAL_MODE = '1';
41+
cls.reset();
42+
reloadContextUtils();
43+
assert.equal(cls.getNamespace('AWSXRay'), undefined);
44+
});
45+
3646
it('should start in automatic mode by creating the X-Ray namespace', function() {
37-
assert.equal(ContextUtils.getNamespace().name, 'AWSXRay');
47+
assert.notEqual(cls.getNamespace('AWSXRay'), undefined);
3848
});
3949

4050
it('should set the contextMissingStrategy to LOG_ERROR by default', function() {

0 commit comments

Comments
 (0)