<p dir="auto">Consider:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="let connection = await Deno.connectTls({hostname: &quot;google.com&quot;, port: 443})

// No ‘await’.
connection.read(new Uint8Array(2048))

await connection.write(new Uint8Array([0x0D, 0x0A]))
console.log(&quot;yo&quot;)"><pre class="notranslate"><span class="pl-k">let</span> <span class="pl-s1">connection</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-v">Deno</span><span class="pl-kos">.</span><span class="pl-en">connectTls</span><span class="pl-kos">(</span><span class="pl-kos">{</span><span class="pl-c1">hostname</span>: <span class="pl-s">"google.com"</span><span class="pl-kos">,</span> <span class="pl-c1">port</span>: <span class="pl-c1">443</span><span class="pl-kos">}</span><span class="pl-kos">)</span>

<span class="pl-c">// No ‘await’.</span>
<span class="pl-s1">connection</span><span class="pl-kos">.</span><span class="pl-en">read</span><span class="pl-kos">(</span><span class="pl-k">new</span> <span class="pl-v">Uint8Array</span><span class="pl-kos">(</span><span class="pl-c1">2048</span><span class="pl-kos">)</span><span class="pl-kos">)</span>

<span class="pl-k">await</span> <span class="pl-s1">connection</span><span class="pl-kos">.</span><span class="pl-en">write</span><span class="pl-kos">(</span><span class="pl-k">new</span> <span class="pl-v">Uint8Array</span><span class="pl-kos">(</span><span class="pl-kos">[</span><span class="pl-c1">0x0D</span><span class="pl-kos">,</span> <span class="pl-c1">0x0A</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">"yo"</span><span class="pl-kos">)</span></pre></div>
<p dir="auto">I’d expect the <code class="notranslate">connection.write(...)</code> promise to resolve eventually, but instead it does not (and the process blocks without ever logging <code class="notranslate">"yo"</code>).</p>
<p dir="auto">The issue seems to be that <code class="notranslate">Deno.startTls()</code> resolves before the connection is fully setup - any writer created on the resulting tlsConnection will fail - here's a test:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import {
  fail
} from &quot;https://deno.land/std@0.83.0/testing/asserts.ts&quot;;
import { BufWriter } from &quot;https://deno.land/std@0.83.0/io/mod.ts&quot;;

Deno.test(&quot;tls - raw tls upgrade&quot;, async () =&gt; {
  interface Deferred&lt;T&gt; extends Promise&lt;T&gt; {
    resolve: (value?: T | PromiseLike&lt;T&gt;) =&gt; void;
    reject: (reason?: any) =&gt; void;
  }

  function deferred&lt;T&gt;(): Deferred&lt;T&gt; {
    let methods = {};
    const p = new Promise&lt;T&gt;((resolve, reject): void =&gt; {
      methods = { resolve, reject };
    });
    return Object.assign(p, methods) as Deferred&lt;T&gt;;
  }

  const te = new TextEncoder();
  const td = new TextDecoder();

  function render(frame: Uint8Array): string {
    const cr = &quot;␍&quot;;
    const lf = &quot;␊&quot;;
    return td.decode(frame)
      .replace(/\n/g, lf)
      .replace(/\r/g, cr);
  }

  async function write(writer: BufWriter, pm: string) {
    try {
      const buf = te.encode(pm);
      const c = await writer.write(buf);
      await writer.flush();
      console.info(`&lt; ${c}/${buf.length}:`, render(buf));
    } catch (err) {
      console.error(`error writing pm: ${pm}: ${err}`);
      throw err;
    }
  }

  const pong = deferred&lt;void&gt;();
  const hostname = &quot;demo.nats.io&quot;;
  let conn = await Deno.connect({ hostname, port: 4443 });
  const buf = new Uint8Array(64 * 1023);

  let c = await conn.read(buf);
  if (c === null) {
    fail(&quot;connection closed&quot;);
  }
  //@ts-ignore - this has to be a number
  const frame = buf.subarray(0, c);
  console.info(&quot;&gt;&quot;, render(frame));

  // attempt the tls upgrade
  console.info(&quot;upgrading connection&quot;);
  const tlsConn = await Deno.startTls(conn, { hostname });
  console.info(&quot;connection upgraded&quot;);

  // start a reader to print server responses
  let _ = (async function () {
    while (true) {
      try {
        const c = await tlsConn.read(buf);
        if (c === null) {
          break;
        }
        if (c) {
          const frame = buf.subarray(0, c);
          console.info(&quot;&gt;&quot;, render(frame));

          const re = /^PONG\r\n/mi;
          if (re.test(td.decode(frame))) {
            pong.resolve();
          }
        }
      } catch (err) {
        // closed
        break;
      }
    }
  })();

  const writer = new BufWriter(tlsConn);
  await write(
    writer,
    `CONNECT {&quot;protocol&quot;:1,&quot;version&quot;:&quot;1.0.0-13&quot;,&quot;lang&quot;:&quot;nats.deno&quot;,&quot;verbose&quot;:false,&quot;pedantic&quot;:true, &quot;verbose&quot;:true}\r\n`,
  );
  await write(writer, `PING\r\n`);
  await pong;
  tlsConn.close();
});"><pre class="notranslate"><code class="notranslate">import {
  fail
} from "https://deno.land/std@0.83.0/testing/asserts.ts";
import { BufWriter } from "https://deno.land/std@0.83.0/io/mod.ts";

Deno.test("tls - raw tls upgrade", async () =&gt; {
  interface Deferred&lt;T&gt; extends Promise&lt;T&gt; {
    resolve: (value?: T | PromiseLike&lt;T&gt;) =&gt; void;
    reject: (reason?: any) =&gt; void;
  }

  function deferred&lt;T&gt;(): Deferred&lt;T&gt; {
    let methods = {};
    const p = new Promise&lt;T&gt;((resolve, reject): void =&gt; {
      methods = { resolve, reject };
    });
    return Object.assign(p, methods) as Deferred&lt;T&gt;;
  }

  const te = new TextEncoder();
  const td = new TextDecoder();

  function render(frame: Uint8Array): string {
    const cr = "␍";
    const lf = "␊";
    return td.decode(frame)
      .replace(/\n/g, lf)
      .replace(/\r/g, cr);
  }

  async function write(writer: BufWriter, pm: string) {
    try {
      const buf = te.encode(pm);
      const c = await writer.write(buf);
      await writer.flush();
      console.info(`&lt; ${c}/${buf.length}:`, render(buf));
    } catch (err) {
      console.error(`error writing pm: ${pm}: ${err}`);
      throw err;
    }
  }

  const pong = deferred&lt;void&gt;();
  const hostname = "demo.nats.io";
  let conn = await Deno.connect({ hostname, port: 4443 });
  const buf = new Uint8Array(64 * 1023);

  let c = await conn.read(buf);
  if (c === null) {
    fail("connection closed");
  }
  //@ts-ignore - this has to be a number
  const frame = buf.subarray(0, c);
  console.info("&gt;", render(frame));

  // attempt the tls upgrade
  console.info("upgrading connection");
  const tlsConn = await Deno.startTls(conn, { hostname });
  console.info("connection upgraded");

  // start a reader to print server responses
  let _ = (async function () {
    while (true) {
      try {
        const c = await tlsConn.read(buf);
        if (c === null) {
          break;
        }
        if (c) {
          const frame = buf.subarray(0, c);
          console.info("&gt;", render(frame));

          const re = /^PONG\r\n/mi;
          if (re.test(td.decode(frame))) {
            pong.resolve();
          }
        }
      } catch (err) {
        // closed
        break;
      }
    }
  })();

  const writer = new BufWriter(tlsConn);
  await write(
    writer,
    `CONNECT {"protocol":1,"version":"1.0.0-13","lang":"nats.deno","verbose":false,"pedantic":true, "verbose":true}\r\n`,
  );
  await write(writer, `PING\r\n`);
  await pong;
  tlsConn.close();
});
</code></pre></div>
<p dir="auto">Running with 1.4.6-&gt;1.6.1:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="❯ deno --version
deno 1.4.6
v8 8.7.220.3
typescript 4.0.3
❯ deno test --allow-all --unstable --filter &quot;tls - raw tls upgrade&quot;
Check file:///Users/xxx/Dropbox/code/src/github.com/nats-io/nats.deno/$deno$test.ts
running 1 tests
test tls - raw tls upgrade ... &gt; INFO {&quot;server_id&quot;:&quot;NDGMFJPAMLNEQNWVI5YRSEYMCPAU2SBIE6SCNE2I26CFRAUYBGSURRCU&quot;,&quot;server_name&quot;:&quot;NDGMFJPAMLNEQNWVI5YRSEYMCPAU2SBIE6SCNE2I26CFRAUYBGSURRCU&quot;,&quot;version&quot;:&quot;2.2.0-beta.35&quot;,&quot;proto&quot;:1,&quot;go&quot;:&quot;go1.15.6&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:4443,&quot;headers&quot;:true,&quot;tls_required&quot;:true,&quot;max_payload&quot;:1048576,&quot;client_id&quot;:382,&quot;client_ip&quot;:&quot;71.193.91.14&quot;} ␍␊
upgrading connection
connection upgraded
&lt; 112/112: CONNECT {&quot;protocol&quot;:1,&quot;version&quot;:&quot;1.0.0-13&quot;,&quot;lang&quot;:&quot;nats.deno&quot;,&quot;verbose&quot;:false,&quot;pedantic&quot;:true, &quot;verbose&quot;:true}␍␊
&lt; 6/6: PING␍␊
&gt; +OK␍␊PONG␍␊
ok (367ms)"><pre class="notranslate">❯ deno --version
deno 1.4.6
v8 8.7.220.3
typescript 4.0.3
❯ deno <span class="pl-c1">test</span> --allow-all --unstable --filter <span class="pl-s"><span class="pl-pds">"</span>tls - raw tls upgrade<span class="pl-pds">"</span></span>
Check file:///Users/xxx/Dropbox/code/src/github.com/nats-io/nats.deno/<span class="pl-smi">$deno$test</span>.ts
running 1 tests
<span class="pl-c1">test</span> tls - raw tls upgrade ... <span class="pl-k">&gt;</span> INFO {<span class="pl-s"><span class="pl-pds">"</span>server_id<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>NDGMFJPAMLNEQNWVI5YRSEYMCPAU2SBIE6SCNE2I26CFRAUYBGSURRCU<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>server_name<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>NDGMFJPAMLNEQNWVI5YRSEYMCPAU2SBIE6SCNE2I26CFRAUYBGSURRCU<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>version<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>2.2.0-beta.35<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>proto<span class="pl-pds">"</span></span>:1,<span class="pl-s"><span class="pl-pds">"</span>go<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>go1.15.6<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>host<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>0.0.0.0<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>port<span class="pl-pds">"</span></span>:4443,<span class="pl-s"><span class="pl-pds">"</span>headers<span class="pl-pds">"</span></span>:true,<span class="pl-s"><span class="pl-pds">"</span>tls_required<span class="pl-pds">"</span></span>:true,<span class="pl-s"><span class="pl-pds">"</span>max_payload<span class="pl-pds">"</span></span>:1048576,<span class="pl-s"><span class="pl-pds">"</span>client_id<span class="pl-pds">"</span></span>:382,<span class="pl-s"><span class="pl-pds">"</span>client_ip<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>71.193.91.14<span class="pl-pds">"</span></span>} ␍␊
upgrading connection
connection upgraded
<span class="pl-k">&lt;</span> 112/112: CONNECT {<span class="pl-s"><span class="pl-pds">"</span>protocol<span class="pl-pds">"</span></span>:1,<span class="pl-s"><span class="pl-pds">"</span>version<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>1.0.0-13<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>lang<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>nats.deno<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>verbose<span class="pl-pds">"</span></span>:false,<span class="pl-s"><span class="pl-pds">"</span>pedantic<span class="pl-pds">"</span></span>:true, <span class="pl-s"><span class="pl-pds">"</span>verbose<span class="pl-pds">"</span></span>:true}␍␊
<span class="pl-k">&lt;</span> 6/6: PING␍␊
<span class="pl-k">&gt;</span> +OK␍␊PONG␍␊
ok (367ms)</pre></div>
<p dir="auto">On 1.6.2+ the result is different - the code will block on the <code class="notranslate">writer.flush()</code>:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="❯ deno test --allow-all --unstable --filter &quot;tls - raw tls upgrade&quot;
Check file:///Users/xxx/Dropbox/code/src/github.com/nats-io/nats.deno/$deno$test.ts
running 1 tests
test tls - raw tls upgrade ... &gt; INFO {&quot;server_id&quot;:&quot;NDGMFJPAMLNEQNWVI5YRSEYMCPAU2SBIE6SCNE2I26CFRAUYBGSURRCU&quot;,&quot;server_name&quot;:&quot;NDGMFJPAMLNEQNWVI5YRSEYMCPAU2SBIE6SCNE2I26CFRAUYBGSURRCU&quot;,&quot;version&quot;:&quot;2.2.0-beta.35&quot;,&quot;proto&quot;:1,&quot;go&quot;:&quot;go1.15.6&quot;,&quot;host&quot;:&quot;0.0.0.0&quot;,&quot;port&quot;:4443,&quot;headers&quot;:true,&quot;tls_required&quot;:true,&quot;max_payload&quot;:1048576,&quot;client_id&quot;:383,&quot;client_ip&quot;:&quot;71.193.91.14&quot;} ␍␊
upgrading connection
connection upgraded
^C"><pre class="notranslate">❯ deno <span class="pl-c1">test</span> --allow-all --unstable --filter <span class="pl-s"><span class="pl-pds">"</span>tls - raw tls upgrade<span class="pl-pds">"</span></span>
Check file:///Users/xxx/Dropbox/code/src/github.com/nats-io/nats.deno/<span class="pl-smi">$deno$test</span>.ts
running 1 tests
<span class="pl-c1">test</span> tls - raw tls upgrade ... <span class="pl-k">&gt;</span> INFO {<span class="pl-s"><span class="pl-pds">"</span>server_id<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>NDGMFJPAMLNEQNWVI5YRSEYMCPAU2SBIE6SCNE2I26CFRAUYBGSURRCU<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>server_name<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>NDGMFJPAMLNEQNWVI5YRSEYMCPAU2SBIE6SCNE2I26CFRAUYBGSURRCU<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>version<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>2.2.0-beta.35<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>proto<span class="pl-pds">"</span></span>:1,<span class="pl-s"><span class="pl-pds">"</span>go<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>go1.15.6<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>host<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>0.0.0.0<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>port<span class="pl-pds">"</span></span>:4443,<span class="pl-s"><span class="pl-pds">"</span>headers<span class="pl-pds">"</span></span>:true,<span class="pl-s"><span class="pl-pds">"</span>tls_required<span class="pl-pds">"</span></span>:true,<span class="pl-s"><span class="pl-pds">"</span>max_payload<span class="pl-pds">"</span></span>:1048576,<span class="pl-s"><span class="pl-pds">"</span>client_id<span class="pl-pds">"</span></span>:383,<span class="pl-s"><span class="pl-pds">"</span>client_ip<span class="pl-pds">"</span></span>:<span class="pl-s"><span class="pl-pds">"</span>71.193.91.14<span class="pl-pds">"</span></span>} ␍␊
upgrading connection
connection upgraded
^C</pre></div>
<p dir="auto">Removing the <code class="notranslate">flush()</code>, will make the program block as the read loop won't ever see the <code class="notranslate">PONG\r\n</code> response it is expecting.</p>