Repository: denoland/deno
File: ext/node/polyfills/http.ts (TypeScript)

Code before:
          this._requestSendError = e;
        },
      });

      this._bodyWritable = writable;
      this._bodyWriter = writable.getWriter();

      this._bodyWriteRid = resourceForReadableStream(readable);
    }

    (async () => {
      let span;
      let context;

      try {
        const parsedUrl = new URL(url);
        const handle = this.socket._handle;
        if (!handle) {
          // Using non-standard socket. There's no way to handle this type of socket.
          // This should be only happening in artificial test cases
          return;
        }

        if (TRACING_ENABLED) {
          span = builtinTracer().startSpan(this.method, { kind: 2 }); // Kind 2 = Client
          context = enterSpan(span);
          span.setAttribute("http.request.method", this.method);
          span.setAttribute("url.full", parsedUrl.href);
          span.setAttribute("url.scheme", parsedUrl.protocol.slice(0, -1));
          span.setAttribute("url.path", parsedUrl.pathname);
          span.setAttribute("url.query", parsedUrl.search.slice(1));
        }

        let baseConnRid = handle[kStreamBaseField][internalRidSymbol];
        if (this._encrypted) {
          [baseConnRid] = op_tls_start({
            rid: baseConnRid,
            hostname: parsedUrl.hostname,
            caCerts: [],
            alpnProtocols: ["http/1.0", "http/1.1"],
          });
        }

        this._req = await op_node_http_request_with_conn(
          this.method,
          url,
          this._createRequestPath(),
          headers,
          this._bodyWriteRid,
          baseConnRid,
          this._encrypted,

Review comment (suggestion):
This is actually mildly problematic, because the correct contexts will not be restored across the `await` points in the block below.

@devsnek do you have any simple ideas for how to resolve this? It is useful to enter the span here, so that request body reads happen within the context of the correct span.

Code after:
        cancel: (e) => {
          this._requestSendError = e;
        },
      });

      this._bodyWritable = writable;
      this._bodyWriter = writable.getWriter();

      this._bodyWriteRid = resourceForReadableStream(readable);
    }

    let span;
    let context;
    (async () => {
      try {
        const parsedUrl = new URL(url);
        const handle = this.socket._handle;
        if (!handle) {
          // Using non-standard socket. There's no way to handle this type of socket.
          // This should be only happening in artificial test cases
          return;
        }

        if (TRACING_ENABLED) {
          span = builtinTracer().startSpan(this.method, { kind: 2 }); // Kind 2 = Client
          context = enterSpan(span);
          span.setAttribute("http.request.method", this.method);
          span.setAttribute("url.full", parsedUrl.href);
          span.setAttribute("url.scheme", parsedUrl.protocol.slice(0, -1));
          span.setAttribute("url.path", parsedUrl.pathname);
          span.setAttribute("url.query", parsedUrl.search.slice(1));
        }

        let baseConnRid = handle[kStreamBaseField][internalRidSymbol];
        if (this._encrypted) {
          [baseConnRid] = op_tls_start({
            rid: baseConnRid,
            hostname: parsedUrl.hostname,
            caCerts: [],
            alpnProtocols: ["http/1.0", "http/1.1"],
          });
        }

        this._req = await op_node_http_request_with_conn(
          this.method,
          url,
          this._createRequestPath(),
          headers,
          this._bodyWriteRid,
          baseConnRid,
          this._encrypted,