From 2dd7fead391572a2d623b7a78bef74d4ef262977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 09:22:43 +0000 Subject: [PATCH 01/13] Wire PythonKeepAlive to PythonClientUser --- P4API.cpp | 4 +--- PythonClientAPI.cpp | 18 +++++++++++++++--- PythonClientAPI.h | 3 ++- PythonKeepAlive.cpp | 5 +++-- PythonKeepAlive.h | 5 ++++- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/P4API.cpp b/P4API.cpp index 657fd09..db42f5f 100644 --- a/P4API.cpp +++ b/P4API.cpp @@ -617,9 +617,7 @@ static PyObject* P4Adapter_setBreak(P4Adapter* self, PyObject* args) { return NULL; } - // Create an object pointer of PythonKeepAlive and pass py_callable - PythonKeepAlive* cb = new PythonKeepAlive(py_callable); - self->clientAPI->SetBreak(cb); + self->clientAPI->SetBreak(py_callable); Py_RETURN_NONE; } diff --git a/PythonClientAPI.cpp b/PythonClientAPI.cpp index 1e4e517..15084a7 100644 --- a/PythonClientAPI.cpp +++ b/PythonClientAPI.cpp @@ -69,6 +69,7 @@ PythonClientAPI::PythonClientAPI() : ui(&debug, &specMgr), specMgr(&debug) { + breakCallback = NULL; server2 = 0; depth = 0; exceptionLevel = 2; @@ -123,11 +124,13 @@ PythonClientAPI::~PythonClientAPI() // can't use logger here, probably already destructed at this time debug.printDebug(P4PYDBG_GC, "Destructor PythonClientAPI::~PythonClientAPI called"); + client.SetBreak(NULL); if( IsConnected() ) { Error e; client.Final( &e ); // Ignore errors } + delete breakCallback; delete enviro; } @@ -724,7 +727,11 @@ PyObject * PythonClientAPI::ConnectOrReconnect() // If an iterator is defined, reset the break functionality // for the KeepAlive function - if( ui.GetHandler() != Py_None ) + if( breakCallback ) + { + client.SetBreak( breakCallback ); + } + else if( ui.GetHandler() != Py_None ) { client.SetBreak( &ui ); } @@ -905,7 +912,9 @@ int PythonClientAPI::SetHandler( PyObject * iterator ) return -1; } - if( iterator == Py_None) + if( breakCallback ) + client.SetBreak( breakCallback ); + else if( iterator == Py_None) client.SetBreak(NULL); else client.SetBreak(&ui); @@ -1303,11 +1312,14 @@ void PythonClientAPI::RunCmd(const char *cmd, ClientUser *ui, int argc, char * c } -void PythonClientAPI::SetBreak(PythonKeepAlive* cb) +void PythonClientAPI::SetBreak(PyObject* callable) { if( IsConnected() ) { debug.debug(P4PYDBG_COMMANDS, "[P4] Establish the callback" ); + PythonKeepAlive *cb = new PythonKeepAlive(&ui, callable); client.SetBreak(cb); + delete breakCallback; + breakCallback = cb; } } diff --git a/PythonClientAPI.h b/PythonClientAPI.h index 191dbc9..64589db 100644 --- a/PythonClientAPI.h +++ b/PythonClientAPI.h @@ -242,7 +242,7 @@ class PythonClientAPI void Except( const char *func, const char *msg, const char *cmd ); // SetBreak - void SetBreak( PythonKeepAlive* cb ); + void SetBreak( PyObject* callable ); public: // setter/getter methods and attributes @@ -321,6 +321,7 @@ class PythonClientAPI private: ClientApi client; // Perforce API Class PythonClientUser ui; + PythonKeepAlive * breakCallback; Enviro * enviro; PythonDebug debug; p4py::SpecMgr specMgr; diff --git a/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index c3993d8..bd29232 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -42,7 +42,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "PythonThreadGuard.h" #include "PythonKeepAlive.h" -PythonKeepAlive::PythonKeepAlive(PyObject* callable) : py_callable(callable) { +PythonKeepAlive::PythonKeepAlive(PythonClientUser* ui, PyObject* callable) + : py_ui(ui), py_callable(callable) { // Increment the reference count for the callable Py_XINCREF(py_callable); } @@ -62,7 +63,7 @@ int PythonKeepAlive::IsAlive() { Py_DECREF(result); return( 0 ); // To terminate the connection } - + return( 1 ); } diff --git a/PythonKeepAlive.h b/PythonKeepAlive.h index 564e879..bbcd070 100644 --- a/PythonKeepAlive.h +++ b/PythonKeepAlive.h @@ -31,15 +31,18 @@ #ifndef PYTHON_KEEP_ALIVE_H #define PYTHON_KEEP_ALIVE_H +class PythonClientUser; + class PythonKeepAlive : public KeepAlive { public: - PythonKeepAlive(PyObject* callable); + PythonKeepAlive(PythonClientUser* ui, PyObject* callable); ~PythonKeepAlive(); virtual int IsAlive() override; virtual int PollMs() override { return 0; } private: + PythonClientUser* py_ui; PyObject* py_callable; }; From c509fcbffe773af09864807a7c0ccf0b057b96af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 10:01:36 +0000 Subject: [PATCH 02/13] Propagate KeepAlive callback exceptions --- PythonClientUser.cpp | 7 +++++++ PythonClientUserHelper.h | 11 +++++++++++ PythonKeepAlive.cpp | 20 ++++++++++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 PythonClientUserHelper.h diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 9facd95..8275b2a 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -51,6 +51,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "SpecMgr.h" #include "P4Result.h" #include "PythonClientUser.h" +#include "PythonClientUserHelper.h" #include "PythonClientAPI.h" #include "P4PythonDebug.h" #include "PythonThreadGuard.h" @@ -122,6 +123,12 @@ void PythonClientUser::SetPendingException(PyObject *type, PyObject *value, PyOb pendingExcTb = tb; } +void PythonClientUserSetPendingException(PythonClientUser *ui, + PyObject *type, PyObject *value, PyObject *tb) +{ + ui->SetPendingException(type, value, tb); +} + bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, PyObject **tb) { if (pendingExcType == NULL) { diff --git a/PythonClientUserHelper.h b/PythonClientUserHelper.h new file mode 100644 index 0000000..e274a34 --- /dev/null +++ b/PythonClientUserHelper.h @@ -0,0 +1,11 @@ +#ifndef PYTHON_CLIENT_USER_HELPER_H +#define PYTHON_CLIENT_USER_HELPER_H + +#include + +class PythonClientUser; + +void PythonClientUserSetPendingException(PythonClientUser *ui, + PyObject *type, PyObject *value, PyObject *tb); + +#endif diff --git a/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index bd29232..8dc62a4 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -40,6 +40,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include "PythonThreadGuard.h" +#include "PythonClientUserHelper.h" #include "PythonKeepAlive.h" PythonKeepAlive::PythonKeepAlive(PythonClientUser* ui, PyObject* callable) @@ -59,12 +60,23 @@ int PythonKeepAlive::IsAlive() { if (py_callable && PyCallable_Check(py_callable)) { PyObject* result = PyObject_CallObject(py_callable, NULL); - if (result != NULL && PyLong_Check(result) && PyLong_AsLong(result) == 0) { - Py_DECREF(result); - return( 0 ); // To terminate the connection + if (result == NULL) { + PyObject *excType, *excValue, *excTb; + PyErr_Fetch(&excType, &excValue, &excTb); + PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + return( 0 ); } - return( 1 ); + int alive = PyObject_IsTrue(result); + Py_DECREF(result); + if (alive < 0) { + PyObject *excType, *excValue, *excTb; + PyErr_Fetch(&excType, &excValue, &excTb); + PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + return( 0 ); + } + + return alive; } return( 1 ); // Default to true if callable is not set or failed From 2663870ec71d838a1efaa5fac23b4108f346a042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 10:30:19 +0000 Subject: [PATCH 03/13] Test KeepAlive KeyboardInterrupt propagation --- p4test.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/p4test.py b/p4test.py index 16acaf1..2187a8d 100644 --- a/p4test.py +++ b/p4test.py @@ -1807,6 +1807,17 @@ def isAlive(self): if self.counter > self.total_count: return 0 return 1 + + class RaisingKeepAlive: + def __init__(self): + self.called = False + + def isAlive(self): + self.called = True + raise KeyboardInterrupt("KeepAlive callback interrupted command") + + def __call__(self): + return self.isAlive() #create a multiple changelist revision for i in range(100): @@ -1913,6 +1924,22 @@ def _run(): _p4d_proc2.kill() _p4d_proc2.wait() + # --- Case 3: callback exceptions propagate out of the command --- + _p4d_proc3, _free_port3 = _start_tcp_p4d() + _p4_tcp3 = _connect_tcp(_free_port3) + raising_ka = RaisingKeepAlive() + _p4_tcp3.setbreak(raising_ka) + try: + with self.assertRaises(KeyboardInterrupt): + _p4_tcp3.run("changes") + self.assertTrue(raising_ka.called, "isAlive callback was not invoked") + finally: + try: + _p4_tcp3.disconnect() + finally: + _p4d_proc3.kill() + _p4d_proc3.wait() + # Replace self.p4 with fresh unconnected object so tearDown skips # disconnect on the stale RSH connection. self.p4 = P4.P4() @@ -1933,6 +1960,14 @@ def _run(): total_files = len(self.p4.run("changes")) self.assertEqual(100, total_files, "Setbreak is not working") self.p4.disconnect() + + self.p4.connect() + raising_ka = RaisingKeepAlive() + self.p4.setbreak(raising_ka) + with self.assertRaises(KeyboardInterrupt): + self.p4.run("changes") + self.assertTrue(raising_ka.called, "isAlive callback was not invoked") + self.p4.disconnect() return def testMergeToolReturnCode(self): From 37210bac531436a416e1338a09d5e820371a58a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 11:07:25 +0000 Subject: [PATCH 04/13] Consult PythonClientUser's break flag before calling the python callback --- PythonClientUser.cpp | 5 +++++ PythonClientUserHelper.h | 1 + PythonKeepAlive.cpp | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 8275b2a..0bd96dd 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -129,6 +129,11 @@ void PythonClientUserSetPendingException(PythonClientUser *ui, ui->SetPendingException(type, value, tb); } +int PythonClientUserIsAlive(PythonClientUser *ui) +{ + return ui->IsAlive(); +} + bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, PyObject **tb) { if (pendingExcType == NULL) { diff --git a/PythonClientUserHelper.h b/PythonClientUserHelper.h index e274a34..449437d 100644 --- a/PythonClientUserHelper.h +++ b/PythonClientUserHelper.h @@ -7,5 +7,6 @@ class PythonClientUser; void PythonClientUserSetPendingException(PythonClientUser *ui, PyObject *type, PyObject *value, PyObject *tb); +int PythonClientUserIsAlive(PythonClientUser *ui); #endif diff --git a/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index 8dc62a4..59644ec 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -58,6 +58,10 @@ int PythonKeepAlive::IsAlive() { EnsurePythonLock guard; + if (!PythonClientUserIsAlive(py_ui)) { + return( 0 ); + } + if (py_callable && PyCallable_Check(py_callable)) { PyObject* result = PyObject_CallObject(py_callable, NULL); if (result == NULL) { From 98b6f53bde4f860e57549966119b96e3f0d3e4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 12:41:36 +0000 Subject: [PATCH 05/13] Handle Progress callback exceptions --- PythonClientAPI.cpp | 37 ++++++++++++----------- PythonClientAPI.h | 1 + PythonClientProgress.cpp | 26 +++++++++++++++- PythonClientProgress.h | 1 + PythonClientUser.h | 4 +++ p4test.py | 65 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 116 insertions(+), 18 deletions(-) diff --git a/PythonClientAPI.cpp b/PythonClientAPI.cpp index 15084a7..23f66e6 100644 --- a/PythonClientAPI.cpp +++ b/PythonClientAPI.cpp @@ -724,17 +724,7 @@ PyObject * PythonClientAPI::ConnectOrReconnect() if ( e.Test() ) Py_RETURN_FALSE; - // If an iterator is defined, reset the break functionality - // for the KeepAlive function - - if( breakCallback ) - { - client.SetBreak( breakCallback ); - } - else if( ui.GetHandler() != Py_None ) - { - client.SetBreak( &ui ); - } + SetBreakHandler(); SetConnected(); Py_RETURN_NONE; @@ -912,12 +902,7 @@ int PythonClientAPI::SetHandler( PyObject * iterator ) return -1; } - if( breakCallback ) - client.SetBreak( breakCallback ); - else if( iterator == Py_None) - client.SetBreak(NULL); - else - client.SetBreak(&ui); + SetBreakHandler(); return 0; } @@ -936,9 +921,27 @@ int PythonClientAPI::SetProgress( PyObject * progress ) return -1; } + SetBreakHandler(); + return 0; } +void PythonClientAPI::SetBreakHandler() +{ + if( breakCallback ) { + client.SetBreak( breakCallback ); + return; + } + + PyObject *handler = ui.GetHandler(); + PyObject *progress = ui.GetProgress(); + bool hasCallback = handler != Py_None || progress != Py_None; + Py_DECREF(handler); + Py_DECREF(progress); + + client.SetBreak( hasCallback ? &ui : NULL ); +} + PyObject * PythonClientAPI::GetProgress() { return ui.GetProgress(); diff --git a/PythonClientAPI.h b/PythonClientAPI.h index 64589db..9285646 100644 --- a/PythonClientAPI.h +++ b/PythonClientAPI.h @@ -285,6 +285,7 @@ class PythonClientAPI void InitFlags() { flags = S_INITIAL_STATE; } void ResetFlags() { flags &= ~S_RESET_MASK; } + void SetBreakHandler(); void SetTag() { flags |= S_TAGGED; } void ClearTag() { flags &= ~S_TAGGED; } diff --git a/PythonClientProgress.cpp b/PythonClientProgress.cpp index 5b32a35..18d8f30 100644 --- a/PythonClientProgress.cpp +++ b/PythonClientProgress.cpp @@ -61,7 +61,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using namespace std; PythonClientProgress::PythonClientProgress(PythonClientUser *clientUser, PyObject * prog, int type) - : ui(clientUser), progress(prog) + : ui(clientUser), progress(prog), got_exception(false) { EnsurePythonLock guard; @@ -72,7 +72,9 @@ PythonClientProgress::PythonClientProgress(PythonClientUser *clientUser, PyObjec else { PyObject *excType, *excValue, *excTb; PyErr_Fetch(&excType, &excValue, &excTb); + got_exception = true; ui->SetPendingException(excType, excValue, excTb); + ui->SetAlive(false); } } @@ -86,6 +88,10 @@ void PythonClientProgress::Description( const StrPtr *desc, int units ) { EnsurePythonLock guard; + if ( got_exception ) { + return; + } + PyObject * res = PyObject_CallMethod( this->progress , (char*) "setDescription", (char*)"si", desc->Text(), units ); if( res ) { Py_DECREF( res ); @@ -93,7 +99,9 @@ void PythonClientProgress::Description( const StrPtr *desc, int units ) else { PyObject *excType, *excValue, *excTb; PyErr_Fetch(&excType, &excValue, &excTb); + got_exception = true; ui->SetPendingException(excType, excValue, excTb); + ui->SetAlive(false); } } @@ -101,6 +109,10 @@ void PythonClientProgress::Total( long total ) { EnsurePythonLock guard; + if ( got_exception ) { + return; + } + PyObject * res = PyObject_CallMethod( this->progress , (char*) "setTotal", (char*)"i", total ); if( res ) { Py_DECREF( res ); @@ -108,7 +120,9 @@ void PythonClientProgress::Total( long total ) else { PyObject *excType, *excValue, *excTb; PyErr_Fetch(&excType, &excValue, &excTb); + got_exception = true; ui->SetPendingException(excType, excValue, excTb); + ui->SetAlive(false); } } @@ -117,11 +131,18 @@ int PythonClientProgress::Update( long pos ) { EnsurePythonLock guard; + if ( got_exception ) { + return 1; + } + PyObject * result = PyObject_CallMethod( this->progress , (char*) "update", (char*)"l", pos ); if( result == NULL ) { // exception thrown PyObject *excType, *excValue, *excTb; PyErr_Fetch(&excType, &excValue, &excTb); + got_exception = true; ui->SetPendingException(excType, excValue, excTb); + ui->SetAlive(false); + // Returning 1 here does not stop the operation by itself. return 1; } else { @@ -143,6 +164,9 @@ void PythonClientProgress::Done( int fail ) PyObject *excType, *excValue, *excTb; PyErr_Fetch(&excType, &excValue, &excTb); ui->SetPendingException(excType, excValue, excTb); + ui->SetAlive(false); } + + got_exception = false; } diff --git a/PythonClientProgress.h b/PythonClientProgress.h index 6574c06..d45d916 100644 --- a/PythonClientProgress.h +++ b/PythonClientProgress.h @@ -49,6 +49,7 @@ class PythonClientProgress : public ClientProgress private: PythonClientUser * ui; PyObject * progress; + bool got_exception; }; diff --git a/PythonClientUser.h b/PythonClientUser.h index aae8a5e..de42db2 100644 --- a/PythonClientUser.h +++ b/PythonClientUser.h @@ -124,6 +124,10 @@ class PythonClientUser: public ClientUser, public KeepAlive { return alive; } + void SetAlive(bool isAlive) + { + alive = isAlive; + } private: PyObject * MkMergeInfo(ClientMerge *m, StrPtr &hint); diff --git a/p4test.py b/p4test.py index 2187a8d..5d735da 100644 --- a/p4test.py +++ b/p4test.py @@ -1168,6 +1168,71 @@ def done(self, fail): "Total does not match position %d <> %d" % (callback.totalFileCount, callback.position)) self.assertEqual(total, callback.position, "Total does not match position %d <> %d" % (total, callback.position)) + + cancel_dir = "progress_cancel" + cancel_absolute_dir = os.path.join(self.client_root, cancel_dir) + os.mkdir(cancel_absolute_dir) + cancel_files = [os.path.join(cancel_absolute_dir, "000_large_file")] + cancel_files.extend( + os.path.join(cancel_absolute_dir, "zzz_file%02d" % index) + for index in range(10) + ) + with open(cancel_files[0], 'w') as f: + f.write('A' * (8 * 1024 * 1024)) + for fname in cancel_files[1:]: + with open(fname, 'w') as f: + f.write('small file\n') + for fname in cancel_files: + self.p4.run_add(fname) + self.p4.run_submit('-dProgress cancellation files') + for fname in cancel_files: + os.remove(fname) + + class CancelOnUpdate(P4.Progress): + def __init__(self): + P4.Progress.__init__(self) + self.updateCalls = 0 + self.exceptionRaised = False + self.callsAfterException = 0 + self.doneCalls = 0 + self.doneAfterException = 0 + + def recordCall(self): + if self.exceptionRaised: + self.callsAfterException += 1 + + def init(self, progress_type): + self.recordCall() + + def setDescription(self, description, unit): + self.recordCall() + + def setTotal(self, total): + self.recordCall() + + def update(self, position): + self.recordCall() + self.updateCalls += 1 + self.exceptionRaised = True + raise RuntimeError("Progress update interrupted sync") + + def done(self, fail): + self.doneCalls += 1 + if self.exceptionRaised: + self.doneAfterException += 1 + self.exceptionRaised = False + + cancel_callback = CancelOnUpdate() + with self.assertRaises(RuntimeError) as cm: + self.p4.run_sync('-f', '-q', '//depot/%s/000_large_file' % cancel_dir, + progress=cancel_callback) + self.assertEqual(str(cm.exception), "Progress update interrupted sync") + self.assertGreater(cancel_callback.updateCalls, 0, + "Progress update callback was not invoked") + self.assertEqual(cancel_callback.callsAfterException, 0, + "Progress callback ran again before Done after an exception") + self.assertGreater(cancel_callback.doneAfterException, 0, + "Progress done callback was skipped after an exception") else: print("Test case testProgress needs a 2012.2+ Perforce Server to run") From 3d097acc63062c70984eba4c6290de5ba6ab4ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 16:50:01 +0000 Subject: [PATCH 06/13] ensure a python exception stays pending after an interrupted transfer --- PythonClientUser.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 0bd96dd..8462682 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -107,7 +107,6 @@ void PythonClientUser::Reset() // input data is untouched alive = 1; // yes, we want data from the server - ClearPendingException(); } void PythonClientUser::SetPendingException(PyObject *type, PyObject *value, PyObject *tb) From d81dc05240ada575172e6ea2bf9102447ee0d0b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 16:51:14 +0000 Subject: [PATCH 07/13] Test progress exception survives reconnect --- p4test.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/p4test.py b/p4test.py index 5d735da..363be66 100644 --- a/p4test.py +++ b/p4test.py @@ -1188,9 +1188,10 @@ def done(self, fail): for fname in cancel_files: os.remove(fname) - class CancelOnUpdate(P4.Progress): + class CancelOnUpdate(P4.Progress, P4.OutputHandler): def __init__(self): P4.Progress.__init__(self) + P4.OutputHandler.__init__(self) self.updateCalls = 0 self.exceptionRaised = False self.callsAfterException = 0 @@ -1224,8 +1225,9 @@ def done(self, fail): cancel_callback = CancelOnUpdate() with self.assertRaises(RuntimeError) as cm: - self.p4.run_sync('-f', '-q', '//depot/%s/000_large_file' % cancel_dir, - progress=cancel_callback) + self.p4.run_sync('-f', '//depot/%s/...' % cancel_dir, + progress=cancel_callback, + handler=cancel_callback) self.assertEqual(str(cm.exception), "Progress update interrupted sync") self.assertGreater(cancel_callback.updateCalls, 0, "Progress update callback was not invoked") @@ -1233,6 +1235,8 @@ def done(self, fail): "Progress callback ran again before Done after an exception") self.assertGreater(cancel_callback.doneAfterException, 0, "Progress done callback was skipped after an exception") + self.assertTrue(os.path.exists(cancel_files[-1]), + "Returning 1 alone unexpectedly aborted sync") else: print("Test case testProgress needs a 2012.2+ Perforce Server to run") From a21a7b3092e9079570bac2e66d4b485b6a1a9ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 17:01:30 +0000 Subject: [PATCH 08/13] Test progress abort precedence over custom keepalive --- p4test.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/p4test.py b/p4test.py index 363be66..23d2b64 100644 --- a/p4test.py +++ b/p4test.py @@ -1237,6 +1237,36 @@ def done(self, fail): "Progress done callback was skipped after an exception") self.assertTrue(os.path.exists(cancel_files[-1]), "Returning 1 alone unexpectedly aborted sync") + + class AlwaysAlive: + def __init__(self): + self.calls = 0 + + def __call__(self): + self.calls += 1 + return True + + keepalive = AlwaysAlive() + self.p4.setbreak(keepalive) + + class RaisingProgress(P4.Progress): + def __init__(self): + P4.Progress.__init__(self) + self.keepaliveCallsAtException = None + + def update(self, position): + self.keepaliveCallsAtException = keepalive.calls + raise RuntimeError("Progress interrupted with custom keepalive") + + raising_progress = RaisingProgress() + with self.assertRaises(RuntimeError) as cm: + self.p4.run_sync('-f', '-q', '//depot/%s/000_large_file' % cancel_dir, + progress=raising_progress) + self.assertEqual(str(cm.exception), "Progress interrupted with custom keepalive") + self.assertGreater(raising_progress.keepaliveCallsAtException, 0, + "Custom KeepAlive was not called before the Progress error") + self.assertEqual(keepalive.calls, raising_progress.keepaliveCallsAtException, + "Internal Progress abort did not suppress the Python KeepAlive callback") else: print("Test case testProgress needs a 2012.2+ Perforce Server to run") From c53cdc4a212d266909bf0a127755bea5b39c5dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 17:53:22 +0000 Subject: [PATCH 09/13] Preserve long progress totals in Python callbacks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientProgress.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/PythonClientProgress.cpp b/PythonClientProgress.cpp index 18d8f30..32505c1 100644 --- a/PythonClientProgress.cpp +++ b/PythonClientProgress.cpp @@ -113,7 +113,7 @@ void PythonClientProgress::Total( long total ) return; } - PyObject * res = PyObject_CallMethod( this->progress , (char*) "setTotal", (char*)"i", total ); + PyObject * res = PyObject_CallMethod( this->progress , (char*) "setTotal", (char*)"l", total ); if( res ) { Py_DECREF( res ); } @@ -169,4 +169,3 @@ void PythonClientProgress::Done( int fail ) got_exception = false; } - From d7095ff1a92376de0fc47cb33e0cf11191cd0831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 19:59:24 +0000 Subject: [PATCH 10/13] Add helpers for pending Python exceptions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientAPI.cpp | 7 ++---- PythonClientProgress.cpp | 28 ++++++++--------------- PythonClientUser.cpp | 48 ++++++++++++++++++++++++++++------------ PythonClientUser.h | 2 ++ 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/PythonClientAPI.cpp b/PythonClientAPI.cpp index 15084a7..4bef180 100644 --- a/PythonClientAPI.cpp +++ b/PythonClientAPI.cpp @@ -833,11 +833,8 @@ PyObject * PythonClientAPI::Run( const char *cmd, int argc, char * const *argv ) } // Check for pending exception from callbacks - PyObject *excType, *excValue, *excTb; - if( ui.GetPendingException(&excType, &excValue, &excTb) ) { - PyErr_Restore(excType, excValue, excTb); - return NULL; - } + if (ui.RaisePendingException()) + return NULL; p4py::P4Result &results = ui.GetResults(); diff --git a/PythonClientProgress.cpp b/PythonClientProgress.cpp index 5b32a35..bf3cfeb 100644 --- a/PythonClientProgress.cpp +++ b/PythonClientProgress.cpp @@ -70,9 +70,7 @@ PythonClientProgress::PythonClientProgress(PythonClientUser *clientUser, PyObjec Py_DECREF( res ); } else { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - ui->SetPendingException(excType, excValue, excTb); + ui->SetPendingCurrentException(); } } @@ -88,12 +86,10 @@ void PythonClientProgress::Description( const StrPtr *desc, int units ) PyObject * res = PyObject_CallMethod( this->progress , (char*) "setDescription", (char*)"si", desc->Text(), units ); if( res ) { - Py_DECREF( res ); + Py_DECREF( res ); } else { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - ui->SetPendingException(excType, excValue, excTb); + ui->SetPendingCurrentException(); } } @@ -103,12 +99,10 @@ void PythonClientProgress::Total( long total ) PyObject * res = PyObject_CallMethod( this->progress , (char*) "setTotal", (char*)"i", total ); if( res ) { - Py_DECREF( res ); + Py_DECREF( res ); } else { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - ui->SetPendingException(excType, excValue, excTb); + ui->SetPendingCurrentException(); } } @@ -119,13 +113,11 @@ int PythonClientProgress::Update( long pos ) PyObject * result = PyObject_CallMethod( this->progress , (char*) "update", (char*)"l", pos ); if( result == NULL ) { // exception thrown - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - ui->SetPendingException(excType, excValue, excTb); - return 1; + ui->SetPendingCurrentException(); + return 1; } else { - Py_DECREF( result ); + Py_DECREF( result ); } return 0; @@ -140,9 +132,7 @@ void PythonClientProgress::Done( int fail ) Py_DECREF( res ); } else { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - ui->SetPendingException(excType, excValue, excTb); + ui->SetPendingCurrentException(); } } diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 0bd96dd..5dd4f85 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -137,6 +137,9 @@ int PythonClientUserIsAlive(PythonClientUser *ui) bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, PyObject **tb) { if (pendingExcType == NULL) { + *type = NULL; + *value = NULL; + *tb = NULL; return false; } @@ -152,6 +155,29 @@ bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, Py return true; } +bool PythonClientUser::SetPendingCurrentException() +{ + if (PyErr_Occurred()) + { + PyObject *excType, *excValue, *excTb; + PyErr_Fetch(&excType, &excValue, &excTb); + SetPendingException(excType, excValue, excTb); + return true; + } + return false; +} + +bool PythonClientUser::RaisePendingException() +{ + PyObject *excType, *excValue, *excTb; + if (GetPendingException(&excType, &excValue, &excTb)) + { + PyErr_Restore(excType, excValue, excTb); + return true; + } + return false; +} + void PythonClientUser::ClearPendingException() { Py_XDECREF(pendingExcType); @@ -189,10 +215,8 @@ bool PythonClientUser::CallOutputMethod( const char * method, PyObject * data) PyObject * result = PyObject_CallMethod( this->handler , (char*) method, (char*)"O", data ); if( result == NULL ) { // exception thrown - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - SetPendingException(excType, excValue, excTb); - alive = 0; + SetPendingCurrentException(); + alive = 0; } else { long a = PyInt_AsLong( result ); @@ -653,11 +677,9 @@ int PythonClientUser::Resolve( ClientMerge *m, Error *e ) PyObject * result = PyObject_CallMethod( this->resolver , (char*)"resolve", (char*)"(O)", mergeData ); if( result == NULL ) { // exception thrown, bug out of here - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - SetPendingException(excType, excValue, excTb); - Py_DECREF(mergeData); - return CMS_QUIT; + SetPendingCurrentException(); + Py_DECREF(mergeData); + return CMS_QUIT; } if (IsString(result)) { @@ -736,11 +758,9 @@ int PythonClientUser::Resolve( ClientResolveA *m, int preview, Error *e ) PyObject * result = PyObject_CallMethod( this->resolver , (char*)"actionResolve", (char*)"(O)", mergeData ); if( result == NULL ) { // exception thrown, bug out of here - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - SetPendingException(excType, excValue, excTb); - Py_DECREF(mergeData); - return CMS_QUIT; + SetPendingCurrentException(); + Py_DECREF(mergeData); + return CMS_QUIT; } StrBuf reply = GetPythonString( result ); diff --git a/PythonClientUser.h b/PythonClientUser.h index aae8a5e..9c8a0a9 100644 --- a/PythonClientUser.h +++ b/PythonClientUser.h @@ -118,6 +118,8 @@ class PythonClientUser: public ClientUser, public KeepAlive void SetPendingException(PyObject *type, PyObject *value, PyObject *tb); bool GetPendingException(PyObject **type, PyObject **value, PyObject **tb); void ClearPendingException(); + bool SetPendingCurrentException(); // returns true if an exception was set + bool RaisePendingException(); // returns true if an exception was raised // override from KeepAlive virtual int IsAlive() From 8e8abd9a383c8c7b3e8ce6307418cff4536e4c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 20:05:06 +0000 Subject: [PATCH 11/13] Use pending exception helper in keepalive callbacks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientUser.cpp | 5 ++--- PythonClientUserHelper.h | 3 +-- PythonKeepAlive.cpp | 8 ++------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 5dd4f85..9f50514 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -123,10 +123,9 @@ void PythonClientUser::SetPendingException(PyObject *type, PyObject *value, PyOb pendingExcTb = tb; } -void PythonClientUserSetPendingException(PythonClientUser *ui, - PyObject *type, PyObject *value, PyObject *tb) +bool PythonClientUserSetPendingCurrentException(PythonClientUser *ui) { - ui->SetPendingException(type, value, tb); + return ui->SetPendingCurrentException(); } int PythonClientUserIsAlive(PythonClientUser *ui) diff --git a/PythonClientUserHelper.h b/PythonClientUserHelper.h index 449437d..c9bb9e0 100644 --- a/PythonClientUserHelper.h +++ b/PythonClientUserHelper.h @@ -5,8 +5,7 @@ class PythonClientUser; -void PythonClientUserSetPendingException(PythonClientUser *ui, - PyObject *type, PyObject *value, PyObject *tb); +bool PythonClientUserSetPendingCurrentException(PythonClientUser *ui); int PythonClientUserIsAlive(PythonClientUser *ui); #endif diff --git a/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index 59644ec..6f1a20f 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -65,18 +65,14 @@ int PythonKeepAlive::IsAlive() { if (py_callable && PyCallable_Check(py_callable)) { PyObject* result = PyObject_CallObject(py_callable, NULL); if (result == NULL) { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + PythonClientUserSetPendingCurrentException(py_ui); return( 0 ); } int alive = PyObject_IsTrue(result); Py_DECREF(result); if (alive < 0) { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + PythonClientUserSetPendingCurrentException(py_ui); return( 0 ); } From 3a5c5ec49b92c02ddbfc62d182c8166ddb45b241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 19:59:24 +0000 Subject: [PATCH 12/13] Add helpers for pending Python exceptions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientAPI.cpp | 7 ++---- PythonClientProgress.cpp | 26 +++++++--------------- PythonClientUser.cpp | 48 ++++++++++++++++++++++++++++------------ PythonClientUser.h | 2 ++ 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/PythonClientAPI.cpp b/PythonClientAPI.cpp index 23f66e6..83cbb38 100644 --- a/PythonClientAPI.cpp +++ b/PythonClientAPI.cpp @@ -823,11 +823,8 @@ PyObject * PythonClientAPI::Run( const char *cmd, int argc, char * const *argv ) } // Check for pending exception from callbacks - PyObject *excType, *excValue, *excTb; - if( ui.GetPendingException(&excType, &excValue, &excTb) ) { - PyErr_Restore(excType, excValue, excTb); - return NULL; - } + if (ui.RaisePendingException()) + return NULL; p4py::P4Result &results = ui.GetResults(); diff --git a/PythonClientProgress.cpp b/PythonClientProgress.cpp index 32505c1..223a820 100644 --- a/PythonClientProgress.cpp +++ b/PythonClientProgress.cpp @@ -70,10 +70,8 @@ PythonClientProgress::PythonClientProgress(PythonClientUser *clientUser, PyObjec Py_DECREF( res ); } else { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); got_exception = true; - ui->SetPendingException(excType, excValue, excTb); + ui->SetPendingCurrentException(); ui->SetAlive(false); } } @@ -94,13 +92,11 @@ void PythonClientProgress::Description( const StrPtr *desc, int units ) PyObject * res = PyObject_CallMethod( this->progress , (char*) "setDescription", (char*)"si", desc->Text(), units ); if( res ) { - Py_DECREF( res ); + Py_DECREF( res ); } else { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); got_exception = true; - ui->SetPendingException(excType, excValue, excTb); + ui->SetPendingCurrentException(); ui->SetAlive(false); } } @@ -115,13 +111,11 @@ void PythonClientProgress::Total( long total ) PyObject * res = PyObject_CallMethod( this->progress , (char*) "setTotal", (char*)"l", total ); if( res ) { - Py_DECREF( res ); + Py_DECREF( res ); } else { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); got_exception = true; - ui->SetPendingException(excType, excValue, excTb); + ui->SetPendingCurrentException(); ui->SetAlive(false); } @@ -137,16 +131,14 @@ int PythonClientProgress::Update( long pos ) PyObject * result = PyObject_CallMethod( this->progress , (char*) "update", (char*)"l", pos ); if( result == NULL ) { // exception thrown - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); got_exception = true; - ui->SetPendingException(excType, excValue, excTb); + ui->SetPendingCurrentException(); ui->SetAlive(false); // Returning 1 here does not stop the operation by itself. return 1; } else { - Py_DECREF( result ); + Py_DECREF( result ); } return 0; @@ -161,9 +153,7 @@ void PythonClientProgress::Done( int fail ) Py_DECREF( res ); } else { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - ui->SetPendingException(excType, excValue, excTb); + ui->SetPendingCurrentException(); ui->SetAlive(false); } diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 8462682..b1e343d 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -136,6 +136,9 @@ int PythonClientUserIsAlive(PythonClientUser *ui) bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, PyObject **tb) { if (pendingExcType == NULL) { + *type = NULL; + *value = NULL; + *tb = NULL; return false; } @@ -151,6 +154,29 @@ bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, Py return true; } +bool PythonClientUser::SetPendingCurrentException() +{ + if (PyErr_Occurred()) + { + PyObject *excType, *excValue, *excTb; + PyErr_Fetch(&excType, &excValue, &excTb); + SetPendingException(excType, excValue, excTb); + return true; + } + return false; +} + +bool PythonClientUser::RaisePendingException() +{ + PyObject *excType, *excValue, *excTb; + if (GetPendingException(&excType, &excValue, &excTb)) + { + PyErr_Restore(excType, excValue, excTb); + return true; + } + return false; +} + void PythonClientUser::ClearPendingException() { Py_XDECREF(pendingExcType); @@ -188,10 +214,8 @@ bool PythonClientUser::CallOutputMethod( const char * method, PyObject * data) PyObject * result = PyObject_CallMethod( this->handler , (char*) method, (char*)"O", data ); if( result == NULL ) { // exception thrown - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - SetPendingException(excType, excValue, excTb); - alive = 0; + SetPendingCurrentException(); + alive = 0; } else { long a = PyInt_AsLong( result ); @@ -652,11 +676,9 @@ int PythonClientUser::Resolve( ClientMerge *m, Error *e ) PyObject * result = PyObject_CallMethod( this->resolver , (char*)"resolve", (char*)"(O)", mergeData ); if( result == NULL ) { // exception thrown, bug out of here - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - SetPendingException(excType, excValue, excTb); - Py_DECREF(mergeData); - return CMS_QUIT; + SetPendingCurrentException(); + Py_DECREF(mergeData); + return CMS_QUIT; } if (IsString(result)) { @@ -735,11 +757,9 @@ int PythonClientUser::Resolve( ClientResolveA *m, int preview, Error *e ) PyObject * result = PyObject_CallMethod( this->resolver , (char*)"actionResolve", (char*)"(O)", mergeData ); if( result == NULL ) { // exception thrown, bug out of here - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - SetPendingException(excType, excValue, excTb); - Py_DECREF(mergeData); - return CMS_QUIT; + SetPendingCurrentException(); + Py_DECREF(mergeData); + return CMS_QUIT; } StrBuf reply = GetPythonString( result ); diff --git a/PythonClientUser.h b/PythonClientUser.h index de42db2..6e105cc 100644 --- a/PythonClientUser.h +++ b/PythonClientUser.h @@ -118,6 +118,8 @@ class PythonClientUser: public ClientUser, public KeepAlive void SetPendingException(PyObject *type, PyObject *value, PyObject *tb); bool GetPendingException(PyObject **type, PyObject **value, PyObject **tb); void ClearPendingException(); + bool SetPendingCurrentException(); // returns true if an exception was set + bool RaisePendingException(); // returns true if an exception was raised // override from KeepAlive virtual int IsAlive() From 69ae15cb09309a1ffce5c70506afa7b3c4da94fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 20:05:06 +0000 Subject: [PATCH 13/13] Use pending exception helper in keepalive callbacks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientUser.cpp | 5 ++--- PythonClientUserHelper.h | 3 +-- PythonKeepAlive.cpp | 8 ++------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index b1e343d..79fd137 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -122,10 +122,9 @@ void PythonClientUser::SetPendingException(PyObject *type, PyObject *value, PyOb pendingExcTb = tb; } -void PythonClientUserSetPendingException(PythonClientUser *ui, - PyObject *type, PyObject *value, PyObject *tb) +bool PythonClientUserSetPendingCurrentException(PythonClientUser *ui) { - ui->SetPendingException(type, value, tb); + return ui->SetPendingCurrentException(); } int PythonClientUserIsAlive(PythonClientUser *ui) diff --git a/PythonClientUserHelper.h b/PythonClientUserHelper.h index 449437d..c9bb9e0 100644 --- a/PythonClientUserHelper.h +++ b/PythonClientUserHelper.h @@ -5,8 +5,7 @@ class PythonClientUser; -void PythonClientUserSetPendingException(PythonClientUser *ui, - PyObject *type, PyObject *value, PyObject *tb); +bool PythonClientUserSetPendingCurrentException(PythonClientUser *ui); int PythonClientUserIsAlive(PythonClientUser *ui); #endif diff --git a/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index 59644ec..6f1a20f 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -65,18 +65,14 @@ int PythonKeepAlive::IsAlive() { if (py_callable && PyCallable_Check(py_callable)) { PyObject* result = PyObject_CallObject(py_callable, NULL); if (result == NULL) { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + PythonClientUserSetPendingCurrentException(py_ui); return( 0 ); } int alive = PyObject_IsTrue(result); Py_DECREF(result); if (alive < 0) { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + PythonClientUserSetPendingCurrentException(py_ui); return( 0 ); }