question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Color not enabled when in a Node Worker

See original GitHub issue

This is related to an ongoing issue/discussion in Node.js related to process.stdout and process.stderr not having isTTY === true when in a Node.js Worker thread.

This means that, if debug is used within a Worker thread, it does not print colors nor ms diff (but timestamps).

Code to reproduce it (requires Node >= 12 or Node 10 with node --experimental-worker flag):

foo.js:

process.env.DEBUG = '*';

const { Worker, parentPort, threadId, isMainThread } = require('worker_threads');
const tty = require('tty');
const debug = require('debug');

async function run()
{
	if (isMainThread)
	{
		const logger = debug('[parent]');

		logger(
			'run() in main thread [process.stderr.isTTY:%s, tty.isatty():%s]',
			process.stderr.isTTY,
			tty.isatty(process.stderr.fd));

		const worker = new Worker(__filename);

		worker.once('message', message => logger('message from child: %o', message));
		worker.postMessage('hi child!');
	}
	else
	{
		const logger = debug('[child]');

		logger(
			'run() in child thread [process.stderr.isTTY:%s, tty.isatty():%s]',
			process.stderr.isTTY,
			tty.isatty(process.stderr.fd));

		parentPort.once('message', message => logger('message from parent: %o', message));
		parentPort.postMessage('hi dad!');
	}
}

run();

Output:

$ node --experimental-worker foo.js

  [parent] run() in main thread [process.stderr.isTTY:true, tty.isatty():true] +0ms
2020-01-12T15:22:36.083Z [child] run() in child thread [process.stderr.isTTY:undefined, tty.isatty():false]
  [parent] message from child: 'hi dad!' +33ms
2020-01-12T15:22:36.085Z [child] message from parent: 'hi child!'

Of course it works if I set DEBUG_COLORS=true environment variable. Just wondering if a debug instance may expose a method to set “tty” mode.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
ibccommented, Jan 12, 2020

The problem is that this may not even be “fixed” in Node. From the referenced issue:

IMO it make sense for .isTTY to be undefined because process.stdout and process.stderr in worker threads don’t actually write to the TTY. They postMessage() the data to the main thread which then writes it out on the worker thread’s behalf.

We could copy the .isTTY values from the main thread but that breaks the idiom where if (process.stdout.isTTY) guards are used around calls to process.stdout.setRawMode(true) or process.stdout.cursorTo() - and you can’t reasonably support those APIs in worker threads because the TTY is a per-process resource.

I’d be happy by just manually setting process.stderr.isTTY = true in the Worker thread. However this won’t make debug module produce colors since it looks at tty.isatty().

And another super ugly workaround is (in the top of the app):

// before loading debug and before creating Workers that also use debug.

const tty = require('tty');
if (tty.isatty(process.stderr.fd)) {
  process.env.DEBUG_COLORS = 'true';
}
1reaction
ibccommented, Jan 12, 2020

Thanks for the explanation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Color not enabled when in a Node Worker · Issue #739 - GitHub
js Worker thread. This means that, if debug is used within a Worker thread, it does not print colors nor ms diff (but...
Read more >
debug - npm
In Node.js, colors are enabled when stderr is a TTY. You also should install the supports-color module alongside debug, otherwise debug will ...
Read more >
Manage nodes in a swarm - Docker Documentation
The MANAGER STATUS column shows node participation in the Raft consensus: No value indicates a worker node that does not participate in swarm...
Read more >
Command-line API | Node.js v19.3.0 Documentation
If the string is not an absolute path, it's resolved as a relative path from the current working directory. That path is then...
Read more >
Nodes stats API | Elasticsearch Guide [8.5] | Elastic
A value of -1 indicates that this is not available. indexing. (object) Contains statistics about indexing operations for the node. Properties of indexing....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found