summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2011-06-06 23:41:31 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2011-06-07 00:08:29 +0100
commit2a1b2b5713ffefbfbb22e95b7182577e3cd212a2 (patch)
tree87367ada9e0f5e1322a4105fee959bcaa7c7244a /scripts
parentdd7ee7093a54213cb5e39fe65a085019f11f5593 (diff)
downloadpsycopg2-2a1b2b5713ffefbfbb22e95b7182577e3cd212a2.tar.gz
Added script to demonstrate the refcount bug during copy
from https://bugzilla.redhat.com/show_bug.cgi?id=711095
Diffstat (limited to 'scripts')
-rw-r--r--scripts/ticket58.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/scripts/ticket58.py b/scripts/ticket58.py
new file mode 100644
index 0000000..86cabac
--- /dev/null
+++ b/scripts/ticket58.py
@@ -0,0 +1,59 @@
+"""
+A script to reproduce the race condition described in ticket #58
+
+from https://bugzilla.redhat.com/show_bug.cgi?id=711095
+
+Results in the error:
+
+ python: Modules/gcmodule.c:277: visit_decref: Assertion `gc->gc.gc_refs != 0'
+ failed.
+
+on unpatched library.
+"""
+
+import threading
+import gc
+import time
+
+import psycopg2
+from StringIO import StringIO
+
+done = 0
+
+class GCThread(threading.Thread):
+ # A thread that sits in an infinite loop, forcing the garbage collector
+ # to run
+ def run(self):
+ global done
+ while not done:
+ gc.collect()
+ time.sleep(0.1) # give the other thread a chance to run
+
+gc_thread = GCThread()
+
+
+# This assumes a pre-existing db named "test", with:
+# "CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);"
+
+conn = psycopg2.connect("dbname=test user=postgres")
+cur = conn.cursor()
+
+# Start the other thread, running the GC regularly
+gc_thread.start()
+
+# Now do lots of "cursor.copy_from" calls:
+for i in range(1000):
+ f = StringIO("42\tfoo\n74\tbar\n")
+ cur.copy_from(f, 'test', columns=('num', 'data'))
+ # Assuming the other thread gets a chance to run during this call, expect a
+ # build of python (with assertions enabled) to bail out here with:
+ # python: Modules/gcmodule.c:277: visit_decref: Assertion `gc->gc.gc_refs != 0' failed.
+
+
+# Terminate the GC thread's loop:
+done = 1
+
+cur.close()
+conn.close()
+
+