Comments (9)
Please ignore the second memory leak. I think I may have imagined it as I cannot replicate. The line I thought was leaking is working fine and is cleaned up by this code further down in the file
Line 1772 in ff1dd23
from pyodbc.
Could you post an ODBC trace?
from pyodbc.
Reproduced on Ubuntu 20.04. Both .rss and .vms increase. Same behaviour if cursor is created, consumed (.fetchall()), and closed for each iteration.
from pyodbc.
ODBC trace logs for "execute with float" and "execute with str", using 5_000 rows. (Trace logs for 1_000_000 rows were almost 500 MiB each unzipped.)
from pyodbc.
Note also that this is not a version 5.x regression; version 4.0.39 produces the same results.
from pyodbc.
No need to run it with that many rows since the pattern shows up with far less.
I think the code is missing a Py_XDECREF(param) after this line: https://github.com/mkleehammer/pyodbc/blob/master/src/params.cpp#L1268
from pyodbc.
I added this to my local build but the leak persists.
@@ -1264,10 +1264,11 @@ bool BindParameter(Cursor* cur, Py_ssize_t index, ParamInfo& info)
for(i=0;i<ncols;i++)
{
// Bind the TVP's columns --- all need to use DAE
PyObject *param = PySequence_GetItem(row, i);
+ Py_XDECREF(param);
GetParameterInfo(cur, i, param, info.nested[i], true);
info.nested[i].BufferLength = info.nested[i].StrLen_or_Ind;
info.nested[i].StrLen_or_Ind = SQL_DATA_AT_EXEC;
Py_BEGIN_ALLOW_THREADS
from pyodbc.
Some more testing that might help.
The leak still occurs if you take the creation of the data
object out of the loop. This seems to imply it is leaking memory allocated within pyODBC rather than a reference count issue with the passed in data. My guess was from the following code in cursor.cpp where it seems like the GetParameterInfo
there is being called for each data item and doing an encode on the string generating an encoded byte array and the cleanup for this is somehow missing (the leak doesn't occur if you provide a bytes
type parameters by doing the encode on the python side but does for str
.)
from pyodbc.
I have found a related memory leak (that I think may have been spotted before) when you insert a long string with cursor.fast_executemany=True
you get a leak if the column you are inserting into is varchar(MAX)
.
Both of these memory leaks are because of the same problem but in two difference places where some coded is duplicated.
The TVP issue is caused because in GetUnicodeInfo
you get a new PyObject
from PyCodec_Encode
with a ref count of 1. While this object is wrapped by encoded
the Detach
call means it will not decrement the ref count in the destructor. For normal calls this is correct behaviour as the decrement happens in FreeInfos
with the line Py_XDECREF(a[i].pObject);
. For TVP parameters FreeInfos
will only clean up the top level parameter and not all the items in the sequence and so this object is getting leaked.
This is the same issue with cursor.fast_executemany=True
when you have a column with MAX size because the detach on this line causes the reference count to not be reduced by the destructor
Line 314 in ff1dd23
from pyodbc.
Related Issues (20)
- im('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: The wait operation timed out.\r\n (258) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 18 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to 10.0.0.5\\MSSQL16.MSSQLSERVER,1454. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (258)')
- Raspberry Pi, Ubuntu 22.04 arm64 ODBC18 SSL error HOT 2
- Can't build release artifacts due to cibuildwheel issue HOT 11
- pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/usr/local/lib/libmyodbc8a.so' : file not found (0) (SQLDriverConnect)") HOT 6
- Linux openSUSE Tumbleweed connection to MariaDB stopped working with version 5.x.x HOT 12
- sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:0A000102:SSL routines::unsupported protocol] (-1) (SQLDriverConnect)') HOT 1
- Connection is busy with results for another command (0) (SQLExecDirectW) HOT 3
- Transaction is not getting rolled back HOT 7
- MACOS 系统使用 HOT 4
- Add support for SQLTablePrivileges
- Login Timeout Error when connecting to Synapse database using pyodbc python library in Azure synapse notebook via Managed Identity HOT 1
- reference count encoding in the connect function HOT 5
- PyToCType uses internal function removed from Python 3.13 HOT 3
- I am trying to install pyodbc in MacBook Pro M3 chip - arm64. The drivers are installed succesfully for odbc 13,17 and 18. But pyodbc is not able to detect the drivers.:
- I am trying to install pyodbc in MacBook Pro M3 chip - arm64. The drivers are installed succesfully for odbc 13,17 and 18. But pyodbc is not able to detect the drivers. ERROR: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)") HOT 5
- Executing multiple queries HOT 1
- pyodbc.connect() gives an error when executing it with pytest HOT 1
- InterfaceError: Data source name not found with pyodbc despite working with isql HOT 8
- Multiple threads querying same table takes a long time HOT 19
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyodbc.