javascript - TypeError: This WritableStream is currently locked to a writer - Stack Overflow

admin2025-05-02  0

I'm experimenting with cloudflare's workers to connect to an SMTP server (port 2525 instead of 25). While trying to use multiple writers by closing one before starting another, I always got the error: TypeError: This WritableStream is currently locked to a writer.. What am I doing wrong? This is my code:

export default {
  async fetch(req){
    const addr = { hostname: "mail.domain.example", port: 2525 };

    try {
      const socket = connect(addr, { secureTransport: "starttls", allowHalfOpen: false });
      const encoder = new TextEncoder();

      let encoded = encoder.encode("EHLO example\r\n");

      const writer1 = socket.writable.getWriter();
      await writer1.write(encoded);
      await writer1.close();

      const writer2 = socket.writable.getWriter();
      await writer2.write(encoded);
      await writer2.close();

    } catch (error) {
      console.log(error);
    }
  }
}

When running this code, it logs the error message. When I run tcpdump on my server, I can see the first EHLO, but not the second one.

I'm experimenting with cloudflare's workers to connect to an SMTP server (port 2525 instead of 25). While trying to use multiple writers by closing one before starting another, I always got the error: TypeError: This WritableStream is currently locked to a writer.. What am I doing wrong? This is my code:

export default {
  async fetch(req){
    const addr = { hostname: "mail.domain.example", port: 2525 };

    try {
      const socket = connect(addr, { secureTransport: "starttls", allowHalfOpen: false });
      const encoder = new TextEncoder();

      let encoded = encoder.encode("EHLO example\r\n");

      const writer1 = socket.writable.getWriter();
      await writer1.write(encoded);
      await writer1.close();

      const writer2 = socket.writable.getWriter();
      await writer2.write(encoded);
      await writer2.close();

    } catch (error) {
      console.log(error);
    }
  }
}

When running this code, it logs the error message. When I run tcpdump on my server, I can see the first EHLO, but not the second one.

Share Improve this question asked Jan 2 at 12:33 left handed polarisationleft handed polarisation 1 2
  • Checkout this GitHub Issue – Sarkis Commented Jan 2 at 13:52
  • I (think I) don't use Qwik, so I think this isn't relevant? – left handed polarisation Commented Jan 2 at 15:35
Add a comment  | 

1 Answer 1

Reset to default 0

You are creating multiple writers from the same WritableStream. The second call to getWriter() fails because the first writer still holds the lock.

You were probably expecting that close() closes the writer, thus releasing the lock, but that's not actually what it does. close() closes the stream itself, that is, sends EOF / FIN / whatever to indicate to the other side that no more data is coming. So even if you could create a new writer, you wouldn't be able to write anything, because the stream is now closed.

I think what you want is to replace the close() call with releaseLock(). This releases the writer so that a new writer can be created.

https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/releaseLock

转载请注明原文地址:http://anycun.com/QandA/1746120034a91944.html