[libcamera-devel,5/5] test: object-invoke: Delete InvokeObject after thread termination

Message ID 20191127084909.10612-5-laurent.pinchart@ideasonboard.com
State Accepted
Commit 803e592cf6ff0c58cdb99c00b1508cefd7d85fe0
Headers show
Series
  • [libcamera-devel,1/5] test: message: Fix message handling in MessageReceiver
Related show

Commit Message

Laurent Pinchart Nov. 27, 2019, 8:49 a.m. UTC
The InvokeObject instance is created on the stack in the run() method,
and is thus destroyed before the thread the object is bound to
terminates. This creates a race condition as the object message handler
could be running when the object is deleted. Fix this by moving the
InvokeObject to a member field.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 test/object-invoke.cpp | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

Comments

Niklas Söderlund Nov. 27, 2019, 3:14 p.m. UTC | #1
Hi Laurent,

Thanks for your patch.

On 2019-11-27 10:49:09 +0200, Laurent Pinchart wrote:
> The InvokeObject instance is created on the stack in the run() method,
> and is thus destroyed before the thread the object is bound to
> terminates. This creates a race condition as the object message handler
> could be running when the object is deleted. Fix this by moving the
> InvokeObject to a member field.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  test/object-invoke.cpp | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/test/object-invoke.cpp b/test/object-invoke.cpp
> index 6582fa75ae11..560adee14e3a 100644
> --- a/test/object-invoke.cpp
> +++ b/test/object-invoke.cpp
> @@ -60,23 +60,22 @@ protected:
>  	int run()
>  	{
>  		EventDispatcher *dispatcher = Thread::current()->eventDispatcher();
> -		InvokedObject object;
>  
>  		/*
>  		 * Test that queued method invocation in the same thread goes
>  		 * through the event dispatcher.
>  		 */
> -		object.invokeMethod(&InvokedObject::method,
> -				    ConnectionTypeQueued, 42);
> +		object_.invokeMethod(&InvokedObject::method,
> +				     ConnectionTypeQueued, 42);
>  
> -		if (object.status() != InvokedObject::NoCall) {
> +		if (object_.status() != InvokedObject::NoCall) {
>  			cerr << "Method not invoked asynchronously" << endl;
>  			return TestFail;
>  		}
>  
>  		dispatcher->processEvents();
>  
> -		switch (object.status()) {
> +		switch (object_.status()) {
>  		case InvokedObject::NoCall:
>  			cout << "Method not invoked for main thread" << endl;
>  			return TestFail;
> @@ -87,7 +86,7 @@ protected:
>  			break;
>  		}
>  
> -		if (object.value() != 42) {
> +		if (object_.value() != 42) {
>  			cout << "Method invoked with incorrect value for main thread" << endl;
>  			return TestFail;
>  		}
> @@ -96,15 +95,15 @@ protected:
>  		 * Move the object to a thread and verify that auto method
>  		 * invocation is delivered in the correct thread.
>  		 */
> -		object.reset();
> -		object.moveToThread(&thread_);
> +		object_.reset();
> +		object_.moveToThread(&thread_);
>  
>  		thread_.start();
>  
> -		object.invokeMethod(&InvokedObject::method,
> -				    ConnectionTypeBlocking, 42);
> +		object_.invokeMethod(&InvokedObject::method,
> +				     ConnectionTypeBlocking, 42);
>  
> -		switch (object.status()) {
> +		switch (object_.status()) {
>  		case InvokedObject::NoCall:
>  			cout << "Method not invoked for custom thread" << endl;
>  			return TestFail;
> @@ -115,7 +114,7 @@ protected:
>  			break;
>  		}
>  
> -		if (object.value() != 42) {
> +		if (object_.value() != 42) {
>  			cout << "Method invoked with incorrect value for custom thread" << endl;
>  			return TestFail;
>  		}
> @@ -131,6 +130,7 @@ protected:
>  
>  private:
>  	Thread thread_;
> +	InvokedObject object_;
>  };
>  
>  TEST_REGISTER(ObjectInvokeTest)
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/test/object-invoke.cpp b/test/object-invoke.cpp
index 6582fa75ae11..560adee14e3a 100644
--- a/test/object-invoke.cpp
+++ b/test/object-invoke.cpp
@@ -60,23 +60,22 @@  protected:
 	int run()
 	{
 		EventDispatcher *dispatcher = Thread::current()->eventDispatcher();
-		InvokedObject object;
 
 		/*
 		 * Test that queued method invocation in the same thread goes
 		 * through the event dispatcher.
 		 */
-		object.invokeMethod(&InvokedObject::method,
-				    ConnectionTypeQueued, 42);
+		object_.invokeMethod(&InvokedObject::method,
+				     ConnectionTypeQueued, 42);
 
-		if (object.status() != InvokedObject::NoCall) {
+		if (object_.status() != InvokedObject::NoCall) {
 			cerr << "Method not invoked asynchronously" << endl;
 			return TestFail;
 		}
 
 		dispatcher->processEvents();
 
-		switch (object.status()) {
+		switch (object_.status()) {
 		case InvokedObject::NoCall:
 			cout << "Method not invoked for main thread" << endl;
 			return TestFail;
@@ -87,7 +86,7 @@  protected:
 			break;
 		}
 
-		if (object.value() != 42) {
+		if (object_.value() != 42) {
 			cout << "Method invoked with incorrect value for main thread" << endl;
 			return TestFail;
 		}
@@ -96,15 +95,15 @@  protected:
 		 * Move the object to a thread and verify that auto method
 		 * invocation is delivered in the correct thread.
 		 */
-		object.reset();
-		object.moveToThread(&thread_);
+		object_.reset();
+		object_.moveToThread(&thread_);
 
 		thread_.start();
 
-		object.invokeMethod(&InvokedObject::method,
-				    ConnectionTypeBlocking, 42);
+		object_.invokeMethod(&InvokedObject::method,
+				     ConnectionTypeBlocking, 42);
 
-		switch (object.status()) {
+		switch (object_.status()) {
 		case InvokedObject::NoCall:
 			cout << "Method not invoked for custom thread" << endl;
 			return TestFail;
@@ -115,7 +114,7 @@  protected:
 			break;
 		}
 
-		if (object.value() != 42) {
+		if (object_.value() != 42) {
 			cout << "Method invoked with incorrect value for custom thread" << endl;
 			return TestFail;
 		}
@@ -131,6 +130,7 @@  protected:
 
 private:
 	Thread thread_;
+	InvokedObject object_;
 };
 
 TEST_REGISTER(ObjectInvokeTest)