Giter Club home page Giter Club logo

Comments (8)

cole avatar cole commented on September 26, 2024

client.is_connected should always be a boolean, and client.connect() shouldn't hang. Any idea what it's hanging on?

You can avoid the whole problem by using the send coroutine and creating a new connection each time, which is probably better practice anyway. But if you do have any more information, please provide it 🙂

from aiosmtplib.

ThirVondukr avatar ThirVondukr commented on September 26, 2024

I'm not sure why client was hanging, that may have something to do with the SMTP server we use, which I have no info about. For now I fixed it by creating a new client each time, which seems to work 🤔

from aiosmtplib.

ThirVondukr avatar ThirVondukr commented on September 26, 2024

Could you explain why creating a new connection would be considered best practice by the way?

from aiosmtplib.

cole avatar cole commented on September 26, 2024

It's just a simple way to avoid a lot of error states from server timeouts, etc.

I wonder if there is an internal lock in the library that's getting stuck, I'll see if I can add some test cases around that.

from aiosmtplib.

ThirVondukr avatar ThirVondukr commented on September 26, 2024

I tried writing a context manager like this:

@contextlib.asynccontextmanager
async def reconnect_smtp_client(
    client: aiosmtplib.SMTP,
    timeout: float = 15,
) -> AsyncIterator[None]:
    try:
        if not client.is_connected:
            logging.warning("SMTP client is not connected, reconnecting.")
            async with asyncio.timeout(timeout):
                await client.connect()
        yield
    except SMTPServerDisconnected:
        logging.warning("Caught SMTPServerDisconnected exception, reconnecting.")
        async with asyncio.timeout(timeout):
            await client.connect()
        raise

But as I said it hanged on client.connect() indefinitely (initially asyncio.timeout wasn't there)

from aiosmtplib.

alex-pirogov avatar alex-pirogov commented on September 26, 2024

im also facing this issue
my idea is to keep SMTP client instance in memory because i dont want to spend time connecting & doing login to a server each time i want to send a message

my steps to reproduce are:

  1. create SMTP client instance
smtp_client = SMTP(...)
  1. connect to an SMTP server (i use mailslurper for development) and send a message
if not smtp_client.is_connected:
   await smtp_client.connect(timeout=10)

smtp_client.send_message(message)
  1. intentionally stop SMTP server
  2. try to repeat step 2. when calling await smtp_client.connect the program hangs infinitely

from aiosmtplib.

ThirVondukr avatar ThirVondukr commented on September 26, 2024

I think that's the exact problem I had

from aiosmtplib.

alex-pirogov avatar alex-pirogov commented on September 26, 2024

i did some digging and found out that this is the place where program hangs
so we need to explicitly release _connect_lock in order to be able to reconnect again
this code seems to work

if not smtp_client.is_connected:
    if smtp_client._connect_lock and smtp_client._connect_lock.locked():
        smtp_client.close()

    await smtp_client.connect()

from aiosmtplib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.