Issue Details (XML | Word | Printable)

Key: PYFB-26
Type: Bug Bug
Status: Closed Closed
Resolution: Cannot Reproduce
Priority: Major Major
Assignee: Pavel Cisar
Reporter: nepomnyashiy evgeniy
Votes: 0
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Firebird driver for Python

TypeError: incompatible types, LP_c_short instance instead of LP_c_short instance

Created: 29/Jan/13 05:51 AM   Updated: 22/Sep/15 08:40 AM
Component/s: None
Affects Version/s: 0.9.9
Fix Version/s: None

Environment: Python 2.7 32bit, apache 2.2.22, mod_wsgi 3.3, Windows server 2003 r2


 Description  « Hide
Sometimes I receive strange exception

  File "c:\tools\reestr\env\Lib\site-packages\fdb\fbcore.py", line 3040, in execute
    PreparedStatement(operation, self, True))
  File "c:\tools\reestr\env\Lib\site-packages\fdb\fbcore.py", line 1701, in __init__
    self.__coerce_XSQLDA(self.out_sqlda)
  File "c:\tools\reestr\env\Lib\site-packages\fdb\fbcore.py", line 2009, in __coerce_XSQLDA
    sqlvar.sqlind = ctypes.pointer(ISC_SHORT(0))
TypeError: incompatible types, LP_c_short instance instead of LP_c_short instance

After I receive it first time, every query to database ends with it. Creating new connection or something else dont help - I need to restart apache.

On my development machine (without apache and mod_wsgi) I never receive such exception.


 All   Comments   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
nepomnyashiy evgeniy added a comment - 08/Mar/13 05:42 AM
On my environment I often received this error (before I rollback to kinterbasdb). May be I can help you? Write some logs or something else?

Pavel Cisar added a comment - 08/Mar/13 07:24 AM
This is clearly Python/ctypes problem, not FDB problem. The FDB code that occasionally reports this bug is unconditionally called whenever SQL command with data is processed, which means that it should always fail if it would be wrong code. Also, nobody else reports such problem although it should be quite common (code path executed frequently), neither could be reproduced. The error message itself is reported by ctypes (standard Python library module) and illogical (LP_c_short instance instead of LP_c_short instance - i.e. expected type although correctly passed was not recognized). So it's clearly problem in your environment or ctypes itself.

I'd suggest to upgrade Python to most recent 2.7 version, preferably from ActiveState (http://www.activestate.com/activepython), and see what happens.

nepomnyashiy evgeniy added a comment - 08/Mar/13 12:35 PM
I use many connection to same db from many threads. With kinterbasdb I set special constant in his init method, but fdb has not this method.
May be problem in such use of fdb?

I already has last python 2.7 version from python.org. I will try to use version from activestate

Pavel Cisar added a comment - 08/Mar/13 01:44 PM
Generally speaking, access from multiple threads shouldn't be a problem, at least if each thread uses it's own Connection object.

KDB was written mostly in C that required special handling of multiple threads due to C/Python interface, while FDB is pure Python. And concurrency at FB API level is handled by FB client library. If you're using FB 2.5, there shouldn't be any problem, see http://www.firebirdsql.org/rlsnotesh/rlsnotes25.html#rnfb25-engine-thrdsafe.

Sam Watkins added a comment - 22/Sep/15 04:07 AM
We have the same bug today on Ubuntu 14.04.2 LTS 64-bit, using fdb 1.4.9 (built from source), Python 2.7.6, and apache2 mod python with mpm prefork. Again, it only happens when running our app in apache, not in the test web server.

An somewhat similar issue with ctypes is reported here, where ctypes reports "incompatible types, X instance instead of X instance":
http://ctypes-users.narkive.com/Dl51mA0r/weird-incompatible-types-error

This error can occur if two different POINTER() types have somehow been created with the same name. But looking through the fdb code, I can't see how it would have happened. You don't create POINTER() types using strings as in the other case.

I am using "subprocess" to fork and run some other programs. I had thought the error might be related to this, but after testing it seems not. Will let you know if we find a solution.

Sam Watkins added a comment - 22/Sep/15 08:40 AM
I hacked around this problem by patching FDB with three ugly casts like this:
2359: sqlvar.sqlind = ctypes.cast(ctypes.pointer(ISC_SHORT(0)), type(sqlvar.sqlind))
2812: sqlvar.sqlind = ctypes.cast(ctypes.pointer(ISC_SHORT(-1)), type(sqlvar.sqlind))
2819: sqlvar.sqlind = ctypes.cast(ctypes.pointer(ISC_SHORT(0)), type(sqlvar.sqlind))

This hack is very far from ideal but it seems to be preventing the exception.

It would be better to fix whatever bug is actually causing us to create two different POINTER(ISC_SHORT) types!
Whether it is in ctypes or fdb, or whatever. Perhaps it's something to do with threading?
I am yet to try this with the latest Python 2.7.10, perhaps they have fixed it.