nodejs / nodejs/node

async_hooks: AsyncLocalStorage losing context

オープン
#41,285 コメント 19 件 リアクション 19 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

async_hooks async_local_storage
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

Version

node v17.3.0 / 16.13.0

Platform

linux (likely all)

Subsystem

async_hooks

What steps will reproduce the bug?

Run

'use strict'

const { AsyncLocalStorage } = require('async_hooks')
const { createServer } = require('http')

const storage = new AsyncLocalStorage()

let counter = 0
createServer((req, res) => {
  const id = counter++;
  console.log('In Middleware with id ' + id);
  storage.run({ id }, function () {
    req.resume()
    req.on('end', onEnd(res))
  });
}).listen(3000)

function onEnd (res) {
  return () => {
    const store = storage.getStore()
    console.log('store is', store)
    res.end(JSON.stringify(store))
  }
}

Then:

curl -d '{}' -H 'Content-Type: application/json' localhost:3000

You'd note that the store is empty.

How often does it reproduce? Is there a required condition?

All of them. Unfortunately the documentation is quite misleading and it assumes the above would work / we do not document the edge cases.

The problem originates on the fact that receiving an HTTP body is part of the http request AsyncRecource. Calling .resume() on it does not imply we are attaching the new storage to that AsyncResource. However our documentation states:

Runs a function synchronously within a context and returns its return value. The store is not accessible outside of the callback function. The store is accessible to any asynchronous operations created within the callback.

Unless somebody knows that calling resume() is not creating a new asynchronous operation, they will be expecting the context to be preserved.

As a further confirmation, the following code works:

'use strict'

const { AsyncLocalStorage } = require('async_hooks')
const { createServer } = require('http')

const storage = new AsyncLocalStorage()

let counter = 0
createServer((req, res) => {
  const id = counter++;
  console.log('In Middleware with id ' + id);
  storage.enterWith({ id })
  req.resume()
  req.on('end', onEnd(res))
}).listen(3000)

function onEnd (res) {
  return () => {
    const store = storage.getStore()
    console.log('store is', store)
    res.end(JSON.stringify(store))
  }
}
What is the expected behavior?

We might decide this is a bug that should be fixed or we might decide to stabilize enterWith and document this case.

The problem is that the current documentation is misleading folks into thinking storage.run() is the right API in all cases but it is not as things are.

What do you see instead?

No response

Additional information

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

HTTP サーバーの再現環境における AsyncLocalStorage の run/enterWith の動作から始め、req.resume() と req.on('end', ...) に焦点を当てます。観測されたコンテキストの伝播を、非同期操作について文書化されている保証と比較します。伝播を変更すべきかどうかをプロジェクトが決定するか、エッジケースと適切な API の動作が文書化されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, node.js
領域
backend
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。