nakagami / pyfirebirdsql

Python DBAPI module for FirebirdSQL
BSD 2-Clause "Simplified" License
71 stars 29 forks source link

Intermittent `AttributeError` in `get_crypt` function on `windows` in multi-threaded code. #104

Closed DeadNews closed 9 months ago

DeadNews commented 9 months ago

Description

Firstly, thank you for your work on this project.

I've encountered an issue with the get_crypt function in firebirdsql/wireprotocol.py.

https://github.com/nakagami/pyfirebirdsql/blob/f2274ae7f1f070234136f8f9ad544096728b7b5d/firebirdsql/wireprotocol.py#L68-L78

On windows, this function occasionally crashes with an AttributeError in multi-threaded code:

...\firebirdsql\wireprotocol.py", line 71, in get_crypt

    return crypt.crypt(plain, '9z')[2:]

           ^^^^^^^^^^^

AttributeError: module 'crypt' has no attribute 'crypt'

Possible Solutions

  1. Import the crypt function directly to avoid the AttributeError:
    try:
        from crypt import crypt
        return crypt(plain, '9z')[2:]
    except ImportError:
        pass
  1. Add AttributeError to the exception handling to catch this error:
    try:
        import crypt
        return crypt.crypt(plain, '9z')[2:]
    except (AttributeError, ImportError):
        pass
  1. Consider not using the crypt module by default, for the reasons described in deprecation notes.

Environment

Additional context

ref: https://github.com/nakagami/pyfirebirdsql/issues/99

nakagami commented 9 months ago

passlib is no longer available!

I appreciate your report. What version of Firebird are you using?

Also, what version of pyfirebirdsql do you use?

DeadNews commented 9 months ago

@nakagami I added this information to the Environment section.

nakagami commented 9 months ago

If you modify as https://github.com/nakagami/pyfirebirdsql/commit/949d7032721caf96a99214443fb398dd3dcb616c and pip install the passlib will it work?

DeadNews commented 9 months ago

https://github.com/nakagami/pyfirebirdsql/blob/949d7032721caf96a99214443fb398dd3dcb616c/firebirdsql/wireprotocol.py#L69-L72

It will crash with an ImportError.

To prevent it from crashing, you can use approaches from point 1 or 2 in the Possible Solutions section.

https://github.com/nakagami/pyfirebirdsql/blob/949d7032721caf96a99214443fb398dd3dcb616c/firebirdsql/wireprotocol.py#L74-L77

Yes, this works on windows with passlib installed.

nakagami commented 9 months ago

Now 1.2.4 released. Please install and check it.