oracle / odpi

ODPI-C: Oracle Database Programming Interface for Drivers and Applications
https://oracle.github.io/odpi/
Other
264 stars 75 forks source link

getting segmentation fault while running multi threaded c application using ODPI library #144

Closed brajncst123 closed 4 years ago

brajncst123 commented 4 years ago
  1. What version of ODPI-C are you using (see dpi.h)?

  2. What OS (and version) is your application on?

  3. What compiler (and version) did you use?

  4. What is your version of the Oracle Client (e.g. Instant Client)? How was it installed? Where it is installed?

  5. What is your Oracle Database version?

  6. What is the PATH environment variable (on Windows) or LD_LIBRARY_PATH (on Linux) set to?

  7. What environment variables did you set? How exactly did you set them?

  8. What problem or error(s) you are seeing?

  9. Do you have a runnable code snippet to describe the problem?

brajncst123 commented 4 years ago
getting segmentation fault while running multi threaded c application select cursor queries using ODPI library

Thread 1 "my-app" received signal SIGSEGV, Segmentation fault. 0x00007ffff7da1926 in dpiVar__initBuffer (var=0x555558e34f50, buffer=0x555558e34f98, error=0x7fffffff5ae0) at src/dpiVar.c:868 868   
src/dpiVar.c: No such file or directory. 
(gdb) n 0x00007ffff4e45370 in skgesig_sigactionHandler () from /usr/lib/oracle/19.6/client64/lib/libclntsh.so (gdb) 
kubo commented 4 years ago

@brajncst123 Could you edit comments to reply questions in the top comment and enclose the gdb output in the second comment with a code block? See https://github.com/kubo/ruby-oci8/issues/195#issuecomment-403250999

I guess that you use one connection in multiple threads at the same time. It isn't allowed. One connection can take one task at a time. You need to create connections equal to at least the number of concurrent executions or use Pool.

EDITED: The above paragraph may not correct. I had problems about multithreading several years ago even when OCIEnv was initialized with OCI_THREADED corresponding to DPI_MODE_CREATE_THREADED in ODPI-C. However it may have been fixed already.

anthony-tuininga commented 4 years ago

Did you make sure that you used mode DPI_MODE_CREATE_THREADED when creating your connection or pool? This should be populated in the dpiCommonCreateParams structure when calling dpiConn_create() or dpiPool_create(). Without it, multithreading applications will definitely crash unless you go through some effort yourself to avoid it!

brajncst123 commented 4 years ago
Please find the stack trace of segmentation fault 

Thread 3 "my-app" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff5754700 (LWP 332492)]
0x00007ffff7da1926 in dpiVar__initBuffer (var=0x7fffd818d760, buffer=0x7fffd818d7a8, error=0x7ffff5746500) at src/dpiVar.c:868
868 src/dpiVar.c: No such file or directory.
(gdb) backtrace 
#0  0x00007ffff7da1926 in dpiVar__initBuffer (var=0x7fffd818d760, buffer=0x7fffd818d7a8, error=0x7ffff5746500) at src/dpiVar.c:868
#1  0x00007ffff7da1dfd in dpiVar__allocate (conn=conn@entry=0x7fffd800c9a0, oracleTypeNum=oracleTypeNum@entry=2021, 
    nativeTypeNum=nativeTypeNum@entry=3010, maxArraySize=maxArraySize@entry=1000, size=1, size@entry=0, sizeIsBytes=sizeIsBytes@entry=0, 
    isArray=0, objType=0x0, var=0x7ffff5746588, data=0x7ffff5746598, error=0x7ffff5746500) at src/dpiVar.c:116
#2  0x00007ffff7d93291 in dpiConn_newVar (conn=0x7fffd800c9a0, oracleTypeNum=2021, nativeTypeNum=3010, maxArraySize=1000, size=0, 
    sizeIsBytes=0, isArray=0, objType=0x0, var=0x7ffff5746588, data=0x7ffff5746598) at src/dpiConn.c:1984
anthony-tuininga commented 4 years ago

Please answer the question I gave you an hour ago as well as the questions from the issue template -- as @kubo suggested. Thanks!

brajncst123 commented 4 years ago
@anthony-tuininga  i putted DPI_MODE_CREATE_THREADED while creating Oracle connection.
Now after that i am getting new issues after running each time my application. 
Some times it works successfully.

My C application is creating multiple threads each thread is reading one table at a time. 
all threads are using same oracle connection.each thread having its own dpiStmt,dpiVar so i am hoping they are independent threads and should not violate in dpi APIs.
There is no global/static data in my application.
anthony-tuininga commented 4 years ago

The Oracle Client library does not support having multiple threads performing work on the same connection simultaneously. If thread A is performing some work on a connection, threads B, C, D, etc. will all block waiting for thread A to complete its work, then one of the waiting threads will begin its work and the other threads will continue waiting, and so forth. If you want to perform concurrent queries or updates you will need to have multiple connections, generally using a session pool (which you can create with dpiPool_create()).

ddevienne commented 4 years ago

And if you want the parallel queries to be read-consistent, you get the OCISnapshot* from the first (lead) connection, which corresponds to the SCN of the transaction that connection is under, and use that same explicit OCIScnapshot in the other connections. I don't know if ODPI exposes that though. I'm interested to know if it does, since I use that in OCI, and have a long term goal of using ODPI instead of raw OCI.

anthony-tuininga commented 4 years ago

ODPI-C does not currently support OCISnapshot*. Please add an enhancement request and provide some sample (OCI) code that demonstrates how you use it.