EC-Nordbund / denomailer

A SMTP-Client implementation for deno (to send mails!)
https://deno.land/x/denomailer
MIT License
52 stars 17 forks source link

Uncaught BadResource Error #19

Closed timonson closed 2 years ago

timonson commented 2 years ago

Describe the bug

The following error is thrown and it can't be caught:

error: Uncaught (in promise) BadResource: Bad resource ID
        nwritten += await this.#writer.write(p.subarray(nwritten));
                    ^
    at async write (deno:ext/net/01_net.js:33:12)
    at async BufWriter.flush (https://deno.land/std@0.130.0/io/buffer.ts:748:21)
    at async SMTPConnection.writeCmd (https://deno.land/x/denomailer@1.0.0/client/basic/connection.ts:108:5)
    at async SMTPClient.send (https://deno.land/x/denomailer@1.0.0/client/basic/client.ts:78:7)

OS: Arch deno version:

mathe42 commented 2 years ago

Could you provide a reproduction and the logs when you set in the client options debug.log to true see https://github.com/EC-Nordbund/denomailer#options

timonson commented 2 years ago

I tried but I can't because it works in a minimal reproduction like expected. Strangely it only happens when I make other http requests through the native http API before making a request which invokes smtp. In this case the send method is stalling before the error occurs. There seems to be some kind of pile-up. If I don't do the other http requests (just normal static file requests), the error doesn't happen.

Only aaa is logged when I make other http requests before:

    console.log("aaa", sendConfig);
    await smtpClient.send(sendConfig);
    console.log("bbb");
    await smtpClient.close();
    console.log("ccc");
mathe42 commented 2 years ago

http-requests = fetch or what?

timonson commented 2 years ago

Automatic browser requests, originiating from an html file. I have a form which invokes smtp on submit. If I don't refresh the site before submitting (because I already visited the page before the server restart), everything works as expected. But when I refresh the site and then submit, the send method is stalling on the server side. The mentioned BadResource Error seems to happen on a subsequent send call.

Oh... so you have also a http-server running in deno...

Yes

mathe42 commented 2 years ago

Oh... so you have also a http-server running in deno...

timonson commented 2 years ago

Edited previous post

mathe42 commented 2 years ago

Are you sure the connection is not closed? If you call close and then send an EMail I would expect that or a similar error.

timonson commented 2 years ago

Yes you are right, that's it and it is working now. I actually closed it intentionally. I must have misunderstood the code in denomailer/client/mod.ts because I thought that the internal client would reconnect automatically. Really sorry for wasting our time. Thanks a lot!

mathe42 commented 2 years ago

With this

import { SMTPClient } from "https://deno.land/x/denomailer@1.0.0/mod.ts";

const client = new SMTPClient({
  debug: {
    log: true,
  },
  connection: {
    hostname: "smtp.gmail.com",
    auth: {
      username: "xxx",
      password: "xxx",
    },
  },
});

await sleep(2000);

await client.close();

await client.send({
  from: "xxx",
  to: "xxx",
  subject: "TEST",
  content: "ABC",
});

function sleep(to: number) {
  return new Promise<void>((res) => setTimeout(res, to));
}

I get the same error so I think your connection got closed!

mathe42 commented 2 years ago

Never mind I fould a small bug while debuging this :D (not related)