Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid reusing nil builder #42

Merged
merged 1 commit into from
Oct 9, 2021
Merged

Conversation

spacewander
Copy link
Member

Fix #41

@spacewander
Copy link
Member Author

@a11enhuang @envestcc
Could you check if this fixes the bug?

@envestcc
Copy link

envestcc commented Oct 8, 2021

@a11enhuang @envestcc Could you check if this fixes the bug?

I have integrated this commit into my project and deployed into testing environment. Because it will take some time to reproduce this problem, i will reply maybe in tomorrow.

@a11enhuang
Copy link

a11enhuang commented Oct 8, 2021 via email

@a11enhuang
Copy link

a11enhuang commented Oct 8, 2021 via email

@spacewander
Copy link
Member Author

@a11enhuang
What's the new stack trace?

@a11enhuang
Copy link

a11enhuang commented Oct 8, 2021 via email

@spacewander
Copy link
Member Author

@a11enhuang
Err... Don't reply image in the email, GitHub only shows the text part.

@a11enhuang
Copy link

a11enhuang commented Oct 8, 2021 via email

@spacewander
Copy link
Member Author

Interesting. We don't call util.PutBuilder in server/server.go:122 after applying this change.

Here is the old stack:

2021/10/8 上午11:03:14 github.com/apache/apisix-go-plugin-runner/internal/util.PutBuilder
2021/10/8 上午11:03:14 /var/lib/jenkins/workspace/build-dev-apple-fuller-apisix-go-plugin-runner/apisix-go-plugin-runner/internal/util/pool.go:37
2021/10/8 上午11:03:14 github.com/apache/apisix-go-plugin-runner/internal/server.handleConn
2021/10/8 上午11:03:14 /var/lib/jenkins/workspace/build-dev-apple-fuller-apisix-go-plugin-runner/apisix-go-plugin-runner/internal/server/server.go:122

Here is the new stack:

github.com/apache/apisix-go-plugin-runner/internal/util.PutBuilder 
	/var/lib/jenkins/workspace/build-dev-apple-fuller-apisix-go-plugin-runner/apisix-go-plugin-runner/internal/util/pool.go:37 
github.com/apache/apisix-go-plugin-runner/internal/server.handleConn 
	/var/lib/jenkins/workspace/build-dev-apple-fuller-apisix-go-plugin-runner/apisix-go-plugin-runner/internal/server/server.go:122

Could you check if the code has the given patch? I already removed the util.PutBuilder in line 122.

diff --git a/internal/server/server.go b/internal/server/server.go
index 48d6da0..6193a11 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -70,12 +70,22 @@ func recoverPanic() {
 	}
 }
 
-func dispatchRPC(ty byte, in []byte, conn net.Conn) (*flatbuffers.Builder, error) {
+func dispatchRPC(ty byte, in []byte, conn net.Conn) *flatbuffers.Builder {
+	var err error
+	var bd *flatbuffers.Builder
 	hl, ok := typeHandlerMap[ty]
 	if !ok {
-		return nil, UnknownType{ty}
+		err = UnknownType{ty}
+	} else {
+		bd, err = hl(in, conn)
+	}
+
+	if err != nil {
+		bd = generateErrorReport(err)
+	} else {
+		bd = checkIfDataTooLarge(bd)
 	}
-	return hl(in, conn)
+	return bd
 }
 
 func checkIfDataTooLarge(bd *flatbuffers.Builder) *flatbuffers.Builder {
@@ -116,15 +126,7 @@ func handleConn(c net.Conn) {
 			break
 		}
 
-		bd, err := dispatchRPC(ty, buf, c)
-
-		if err != nil {
-			util.PutBuilder(bd)
-			bd = generateErrorReport(err)
-		} else {
-			bd = checkIfDataTooLarge(bd)
-		}
-
+		bd := dispatchRPC(ty, buf, c)
 		out := bd.FinishedBytes()
 		size := len(out)
 		binary.BigEndian.PutUint32(header, uint32(size))

@envestcc
Copy link

envestcc commented Oct 8, 2021

@a11enhuang @envestcc Could you check if this fixes the bug?

I have integrated this commit into my project and deployed into testing environment. Because it will take some time to reproduce this problem, i will reply maybe in tomorrow.

Until now, the panic did not happen again. but sometimes the request is directly write to upstream, don't pass to my external-go-plugin, with only a log msg like "WARN server/server.go:59 key not found"

@spacewander
Copy link
Member Author

@envestcc
Let's discuss the issue there: #43

@a11enhuang
Copy link

a11enhuang commented Oct 9, 2021 via email

@spacewander spacewander merged commit 9b2683c into apache:master Oct 9, 2021
@spacewander spacewander deleted the fsv branch October 9, 2021 04:22
@spacewander
Copy link
Member Author

@a11enhuang
I just merge this change into the master. Please ensure the commit hash is 9b2683c and its content contains the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: invalid memory address or nil pointer dereference
4 participants