Javascript / Node中从不执行用户代码的隐藏线程是可能的,如果是这样,它会导致竞争条件的奥秘可能性? [英] Hidden threads in Javascript/Node that never execute user code: is it possible, and if so could it lead to an arcane possibility for a race condition?

查看:163
本文介绍了Javascript / Node中从不执行用户代码的隐藏线程是可能的,如果是这样,它会导致竞争条件的奥秘可能性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据评论/答案查看更新的底部:这个问题是关于不执行回调的隐藏线程的可能性。






我有一个关于潜在的奥术场景的问题,涉及节点请求模块,其中:





  • ... 之前 / em>




我发布这主要是一个健全检查只是为了确保我不会误解Node / JS /请求模块代码的东西。



从请求模块中的示例(参见第二个例子这是:

  //从
//中的第二个示例复制粘贴Node请求库文档,在这里:
// https://www.npmjs.com/package/request#examples

// ...我的ARCANE SCENARIO注入中间

var request = require('request')
request(
{method:'GET'
,uri:'http://www.google.com'
,gzip:true
}
,函数(错误,响应,正文){
//正文是解压缩的响应主体
console.log数据为:'+(response.headers ['content-encoding'] || 'identity'))
console.log('解码的数据是:'+ body)
}


// ******* ********************************************* //
//以下情况可能吗?
//
//< - Hang Hang Hang Hang Hang Hang Hang HANG - >
//
//让我们假设当前线程HANGS在这里,
//但是请求有时间被发送,
//并且响应正在等待由线程接收
//
//< - Hang Hang Hang Hang Hang Hang HANG - >
// ******************************************** ******** //

.on('data',function(data){
//解压缩后接收的数据
console.log 'decode chunk:'+ data)
})
.on('response',function(response){
// unmodified http.IncomingMessage object
response.on数据',函数(数据){
//收到的压缩数据
console.log('received'+ data.length +'bytes of compressed data')
})
})



我在代码片段中指明了我的神秘场景。



假设Node进程在指定的点挂起,但是该内部的(在隐藏的线程中,Javascript不可见,因此不调用 / em> callbacks)WAS能够构造请求,并通过网络发送它;假设挂起持续直到接收到响应(例如,两个块)并等待由节点处理。 (这是一个肯定是奥术的场景,我不确定甚至在理论上是可能的。)



然后假设hang结束,醒来。此外,假设(以某种方式)节点能够将响应一直处理到上面的代码中执行回调函数的点(但是没有移动经过原始代码路径中的代码中的挂起点) ,如果这在理论上是可能的)。



上述奥术场景在理论上是可能的吗?如果是这样,则在对象上安排'data'事件之前,不会通过网络接收数据分组并组合,准备传递到回调函数?在这种情况下,如果可能,我会想象'data'事件将被遗漏。



,我知道这是一个奥术场景 - 或许从理论上讲,根据内部机制和编码,它是不可能的。



这是我的问题 - 是上述奥术场景,其极不可能的种族条件,但理论上可能吗?



我只是为了确保我不缺少一些关键点。感谢。






UPDATE :从评论&答案:我现在已经澄清了我的问题。 'arcane场景'将要求有一个HIDDEN线程(因此不能执行任何USER代码,包括CALLBACKS)构造请求,通过网络发送它,并接收响应 - 没有任何回调触发,包括'data'回调 - 并且在'响应回调准备好被调用时停止,等待(单个)可见的JS线程唤醒。

解决方案

不,这不会发生。
$ b

是的,确实有隐藏后台线程对异步方法工作,但不调用回调。所有执行的javascript确实发生在同一个线程,同步,顺序。 data 事件回调将始终异步执行,即当前脚本/函数运行完成后。



虽然在回调创建并附加到事件发射器之前,确实已经从网络到达数据包,但是在发送请求之前始终创建侦听最低级别数据包的回调是本地makeRequest方法的参数,可以从头开始调用。因此,当数据包在当前脚本(仍被构造事件发射器和附加处理程序占用)完成之前到达时,此事件将排队,并且回调将仅在事件循环后执行准备 - 下一回合。到那时, data 事件回调肯定已创建并附加。


See bottom of question for an update, based on comments/answers: This question is really about the possibility of hidden threads that do not execute callbacks.


I have a question about a potential arcane scenario involving the Node Request module in which:

  • A complete HTTP request is constructed and executed over the network (taking however many ms or even seconds)

  • ... before a single function is executed at runtime on the local machine (typically in the nanoseconds?) - see below for details

I am posting this mostly as a sanity check just to make sure I am not misunderstanding something about Node / JS / Request module code.

From the examples in the Request module (see the SECOND example in that section), is this:

// Copied-and-pasted from the second example in the 
// Node Request library documentation, here:
// https://www.npmjs.com/package/request#examples

// ... My ARCANE SCENARIO is injected in the middle

var request = require('request')
  request(
    { method: 'GET'
    , uri: 'http://www.google.com'
    , gzip: true
    }
  , function (error, response, body) {
      // body is the decompressed response body 
      console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
      console.log('the decoded data is: ' + body)
    }
  )

    // **************************************************** //
    // Is the following scenario possible?
    //
    // <-- HANG HANG HANG HANG HANG HANG HANG HANG HANG -->
    //
    // Let us pretend that the current thread HANGS here,
    // but that the request had time to be sent,
    // and the response is pending being received by the thread
    //
    // <-- HANG HANG HANG HANG HANG HANG HANG HANG HANG -->
    // **************************************************** //

.on('data', function(data) {
    // decompressed data as it is received 
    console.log('decoded chunk: ' + data)
  })
  .on('response', function(response) {
    // unmodified http.IncomingMessage object 
    response.on('data', function(data) {
      // compressed data as it is received 
      console.log('received ' + data.length + ' bytes of compressed data')
    })
  })

I have indicated my arcane scenario in the code snippet.

Suppose the Node process hangs at the point indicated, but that Node internally (in a hidden thread, invisible to Javascript, and therefore not calling any callbacks) WAS able to construct the request, and send it over the network; suppose the hang continues until a response (in two chunks, say) is received and waiting to be processed by Node. (This is the scenario that is certainly arcane, and that I'm not sure is even theoretically possible.)

Then suppose that the hang ends, and the Node thread above wakes up. Further, suppose that (somehow) Node was able to process the response all the way to the point of executing the callback function in the code above (yet without moving past the 'hanged' point in the code in the original code path -- again, if this is even theoretically possible).

Is the above arcane scenario theoretically possible? If so, wouldn't the data packets be received over the network and combined, ready to be passed to the callback function, before the 'data' event was scheduled on the object? In this case, if it's possible, I would imagine that the 'data' event would be missed.

Again, I understand that this is an arcane scenario - perhaps it's not even theoretically possible, given the internal mechanisms and coding involved.

That is my question - is the above arcane scenario, with its extremely unlikely race condition, nonetheless theoretically possible?

I ask just to make sure I'm not missing some key point. Thanks.


UPDATE: From comments & answers: I now have clarified my question. The 'arcane scenario' would require that there is a HIDDEN thread (which therefore CANNOT execute any USER code, including CALLBACKS) that constructs the request, sends it over the network, and receives the response - WITHOUT having any callbacks to trigger, including the 'data' callback - and stops short just at the point that the 'response' callback is ready to be called, waiting for the (single) visible JS thread to wake up.

解决方案

No, this cannot happen.

Yes, there are indeed "hidden" background threads that do the work for asychronous methods, but those don't call callbacks. All execution of javascript does happen on the same thread, synchronously, sequentially. That data event callback will always be executed asynchronously, that is, after the current script/function ran to completion.

While there could indeed already arrive packets from the network before the callback is created and attached to the event emitter, the callback that listens for packets on the lowest level is always created before the request is sent - it is an argument to the native "makeRequest" method, and is available to be called right from the beginning. So when a packet does arrive before the current script (still being occupied by constructing event emitters and attaching handlers) has finished, this event is queued up, and the callback will only be executed after the event loop is ready - on the next turn. By then, the data event callback is certainly created and attached.

这篇关于Javascript / Node中从不执行用户代码的隐藏线程是可能的,如果是这样,它会导致竞争条件的奥秘可能性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆