You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2026/08/21 13:11:45 UTC

(plc4x) branch develop updated: fix(plc4go): report a timed-out request as a timeout, not an internal error

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 57a99fd0fc fix(plc4go): report a timed-out request as a timeout, not an internal error
57a99fd0fc is described below

commit 57a99fd0fcf59858825d7e5c6ba0e6962e3339c2
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Aug 21 14:46:01 2026 +0200

    fix(plc4go): report a timed-out request as a timeout, not an internal error
    
    A request that runs out of time says so in two ways at once: the request context
    reaches its deadline, and the codec's expectation for the response expires - the
    expectation's time to live is derived from that same deadline. Both arms are
    ready together in the same select, so which one a caller observes is a coin
    toss.
    
    The three drivers deciding what to report then asked the context whether it was
    done yet, which on the expectation arm it frequently was not, and a plain
    timeout came back as INTERNAL_ERROR. Read the outcome off the error instead.
    
    Reproduced at 21 failures in 300 runs of the slmp end-to-end test; 0 in 300
    after. IsTimeoutError carries its own test, because the end-to-end one only
    catches this seven times in a hundred.
---
 plc4go/internal/abeth/Reader.go |  2 +-
 plc4go/internal/slmp/Reader.go  |  2 +-
 plc4go/internal/slmp/Writer.go  |  2 +-
 plc4go/spi/utils/Errors.go      | 13 +++++++++++++
 plc4go/spi/utils/Errors_test.go | 26 ++++++++++++++++++++++++++
 5 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/plc4go/internal/abeth/Reader.go b/plc4go/internal/abeth/Reader.go
index cd9503676e..e64b39e97c 100644
--- a/plc4go/internal/abeth/Reader.go
+++ b/plc4go/internal/abeth/Reader.go
@@ -142,7 +142,7 @@ func (m *Reader) readSingleTag(ctx context.Context, tagName string, tag PlcTag)
 		})
 	if err != nil {
 		m.log.Warn().Err(err).Str("tagName", tagName).Msg("error reading tag")
-		if requestCtx.Err() != nil && ctx.Err() == nil {
+		if utils.IsTimeoutError(err) && ctx.Err() == nil {
 			return apiModel.PlcResponseCode_REQUEST_TIMEOUT, nil
 		}
 		return apiModel.PlcResponseCode_INTERNAL_ERROR, nil
diff --git a/plc4go/internal/slmp/Reader.go b/plc4go/internal/slmp/Reader.go
index a7ef99f4d2..ff056377e8 100644
--- a/plc4go/internal/slmp/Reader.go
+++ b/plc4go/internal/slmp/Reader.go
@@ -108,7 +108,7 @@ func (m *Reader) readSingleTag(ctx context.Context, tagName string, tag PlcTag)
 	response, err := sendTransactedRequestAndWait(requestCtx, m.log, m.messageCodec, m.tm, "read", frame)
 	if err != nil {
 		m.log.Warn().Err(err).Str("tagName", tagName).Msg("error reading tag")
-		if requestCtx.Err() != nil && ctx.Err() == nil {
+		if utils.IsTimeoutError(err) && ctx.Err() == nil {
 			// plc4j reports a timeout as REMOTE_ERROR because its PlcResponseCode enum is what it
 			// is; plc4go has a code that says what actually happened.
 			return apiModel.PlcResponseCode_REQUEST_TIMEOUT, nil
diff --git a/plc4go/internal/slmp/Writer.go b/plc4go/internal/slmp/Writer.go
index 84a80ea658..8140508b51 100644
--- a/plc4go/internal/slmp/Writer.go
+++ b/plc4go/internal/slmp/Writer.go
@@ -108,7 +108,7 @@ func (m *Writer) writeSingleTag(ctx context.Context, tagName string, tag PlcTag,
 	response, err := sendTransactedRequestAndWait(requestCtx, m.log, m.messageCodec, m.tm, "write", frame)
 	if err != nil {
 		m.log.Warn().Err(err).Str("tagName", tagName).Msg("error writing tag")
-		if requestCtx.Err() != nil && ctx.Err() == nil {
+		if utils.IsTimeoutError(err) && ctx.Err() == nil {
 			// A write that timed out may still have been applied by the device - the driver cannot
 			// tell, because a 3E frame has no correlation id - so a caller that retries it can
 			// apply it twice. plc4j carries the same caveat.
diff --git a/plc4go/spi/utils/Errors.go b/plc4go/spi/utils/Errors.go
index 52014853b7..7340445414 100644
--- a/plc4go/spi/utils/Errors.go
+++ b/plc4go/spi/utils/Errors.go
@@ -20,6 +20,8 @@
 package utils
 
 import (
+	"context"
+	"errors"
 	"fmt"
 	"time"
 )
@@ -72,3 +74,14 @@ func (t TimeoutError) Is(target error) bool {
 	_, ok := target.(TimeoutError)
 	return ok
 }
+
+// IsTimeoutError reports whether a request failed because it ran out of time.
+//
+// A request that times out has two ways of saying so, and they say it at the same moment: the
+// request context reaches its deadline, and the codec's expectation for the response expires - the
+// expectation's time to live is derived from that very deadline. Whichever of the two a caller
+// happens to observe is therefore a race, so the outcome has to be read off the error rather than
+// off whether the context has got round to marking itself done.
+func IsTimeoutError(err error) bool {
+	return errors.Is(err, TimeoutError{}) || errors.Is(err, context.DeadlineExceeded)
+}
diff --git a/plc4go/spi/utils/Errors_test.go b/plc4go/spi/utils/Errors_test.go
index f4a2fd240a..adbea47549 100644
--- a/plc4go/spi/utils/Errors_test.go
+++ b/plc4go/spi/utils/Errors_test.go
@@ -20,6 +20,8 @@
 package utils
 
 import (
+	"context"
+	"fmt"
 	"testing"
 	"time"
 
@@ -223,3 +225,27 @@ func TestTimeoutError_Is(t1 *testing.T) {
 		})
 	}
 }
+
+// A request that runs out of time reports it either as the codec's expectation expiring or as the
+// request context reaching its deadline, and which of the two a caller sees is a race. Both have to
+// read as a timeout, and nothing else may.
+func TestIsTimeoutError(t *testing.T) {
+	tests := []struct {
+		name string
+		err  error
+		want bool
+	}{
+		{"the codec's expectation expiring", NewTimeoutError(250 * time.Millisecond), true},
+		{"the request context's deadline", context.DeadlineExceeded, true},
+		{"either one, wrapped by a caller", fmt.Errorf("error waiting for read response: %w", NewTimeoutError(time.Second)), true},
+		{"a deadline wrapped by a caller", fmt.Errorf("read aborted: %w", context.DeadlineExceeded), true},
+		{"a cancelled request is not a timeout", context.Canceled, false},
+		{"an unrelated failure is not a timeout", fmt.Errorf("connection reset"), false},
+		{"no error at all", nil, false},
+	}
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			assert.Equal(t, test.want, IsTimeoutError(test.err))
+		})
+	}
+}