1

After reading this question: Do zombies exist ... in .NET? I started to wonder if there is any chance of a zombie process to happen in a javascript code?

Intentional Example:

var f = function(){if(!window) alert('Wont ever happen')}; setInterval(f, 4); 
2

2 Answers 2

5

Javascript is a singlethreaded language which generally only uses local variables if developed properly. It cannot refer to external resources in the same way as .Net does, because it's sandboxed inside the browser. It cannot call local files on its own, and only downloads copies of remote files. There is a filesystem API, but this is also sandboxed. And the Filereader API itself can only handle blobs (i.e. copies of files, not references to files or the files themselves). As such, barring future development in Filesystem access for Javascript, zombie threads aren't really an issue in Javascript.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't agree with the answer given.

Where I do agree is that within the context of JavaScript, "zombie processes" don’t exist in the same way they do in lower-level languages like C++ or environments like .NET, where an actual OS-level process or thread can become orphaned and continue running indefinitely. It is also unfair to compare Javascript with a language like C++ or .Net.

Given the OP's example, zombie-like behaviors can occur in JavaScript if you define them as tasks or functions continuing to execute even after their results are no longer needed.

I can come up with several techniques to create "zombie"-like behaviours:

  • You will never be able to stop this interval if you don't own the interval id.
let counter = 0; setInterval(() => { console.log(`c: ${++counter}`); }, 1000); 
  • An asynchronous operation never cancelled.
(async () => { while (true) { console.log('fetching data...'); // simulate an endless task await new Promise((r) => setTimeout(r, 1000)); } })(); 
  • Overlapping retries
  • Event listeners not being removed
  • Deferred promises without resolution
const deferred = new Promise(() => { // never resolve or reject }); // this will never execute deferred.then(() => console.log('resolved!')); 
  • And the simplest, endless loop in an IIFE
(() => { while(true) { // you will never (ever) be able to interrupt this function. console.log('I never stop'); } })(); 

Because Javascript lacks the implementation of interrupting a function (or the execution of it), "zombie" functions/processes/whatever, are quite easily made and is part of the web since Javascript became mainstream.

Workers (ServiceWorkers, WebWorkers, Worklets) try to simulate behavior common to other languages, but it comes with tremendous limitations.

It is simply the lack of implementation Javascript offers to stop a function from execution, the following is wishful thinking:

const fn = () => { while(true) { console.log('I never stop'); } }; // how nice it would be to receive some sort of an id. const functionId = Function.call(fn); stopFunction(functionId); // or a proxy to signal the function to stop const proxy = Function.call(fn); proxy.interrupt(); 

But, reality is a b...h ;)

1 Comment

Amazing answer, this question was posted in 2013 and since then we had a lot of changes (like nodejs and several new browser apis). A clear definition of a "zombie thread" would be required to improve the question, but your answer is on point. Good job.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.