-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
fatal error: sync: unlock of unlocked mutex (v8.5.0 stmtctx.go:803) #58600
Comments
Should be triggered by some query on information_schema.xxx tables, and that query lead to tidb coprocessor request ... |
Seems we need add |
Thank you for your comments. In StatementContext, mu is not a pointer but an actual instance, and it is not exported either packages. Despite that, I can't figure out why the Unlock call isn't matching. What possible reasons could there be for this? |
I think it same as #57799 metioned. We didn't check |
I think I'm starting to understand this a bit. Are you suggesting that StatementContext is being duplicated outside the package while still containing mu? If that's the case, would it be enough to modify the code (similarly to #57986 ) so that at the beginning of the function, we store the pointer to the mutex in a local variable and then use that? |
You mean #57799? I think it's a workaround way. But we need modify a lots functions, such like |
Sorry, It's TYPO. I will try to modify all functions in stmtctx.go . |
/cc @tiancaiamao |
I’ve committed my changes and opened PR #58629. This is my first contribution, so please let me know if there are any issues or anything I need to fix. |
I’d like to gain a deeper understanding of this issue. As a workaround, I’ve store a pointer of the Mutex to ensure that the target mutex cannot be replaced. However, I believe the real cause is that StatementContext itself is being overwritten by some operation, like duplication—am I correct in thinking this? That said, I don’t currently have enough knowledge to make that judgment on my own 😢 . |
I think you understand it correctly. So I think the better way to fix this issue is to add |
I see. |
I see where mu is being replaced. What if we lock the old Mutex before it gets replaced?
|
I think it will block the groutine for a while and influence the SQL execution. |
I’m also concerned about the performance impact. Should we go ahead with the workaround in the commit, or is there a more appropriate method? What would be the best approach here? |
I have a patch to fix this issue, but I haven't submitted a PR because I haven't found a way to reproduce the panic and can't 100% guarantee the effectiveness of the patch. diff --git a/pkg/executor/infoschema_reader.go b/pkg/executor/infoschema_reader.go
index 206ebb52fb..e203886e58 100644
--- a/pkg/executor/infoschema_reader.go
+++ b/pkg/executor/infoschema_reader.go
@@ -1839,7 +1839,11 @@ func (e *memtableRetriever) setDataForProcessList(ctx sessionctx.Context) {
continue
}
+ if pi.StmtCtx != nil && pi.RefCountOfStmtCtx != nil && !pi.RefCountOfStmtCtx.TryIncrease() {
+ continue
+ }
rows := pi.ToRow(ctx.GetSessionVars().StmtCtx.TimeZone())
+ pi.RefCountOfStmtCtx.Decrease()
record := types.MakeDatums(rows...)
records = append(records, record)
} |
I see! So the StatementContext is only reset in InitStatementContext(), and that is also checked by RefCountOfStmtCtx. tidb/pkg/sessionctx/variable/session.go Lines 1836 to 1848 in de2b7ac
In other words, rather than acquiring the mutex lock within Reset(), we can properly handle this by appropriately managing RefCountOfStmtCtx in the code that calls functions like AffectedRows(). Is that correct? |
I think so. But we couldn't get RefCountOfStmtCtx in |
Based on what I’ve seen, in my environment, every occurrence of the panic happened during calls originating from setDataForProcessList (infoschema_reader.go:1843). I’ll try running the patch you provided on my local environment. If it works, should we skip the previously committed workaround and only introduce the handling of RefCountOfStmtCtx? |
I think it is enough to keep |
I wish you a happy New Year. Since last night, I’ve deployed a binary that includes the patch referencing I’ll keep observing it for a while. |
Does panic still exist? If not, you could update your PR and I will ask my colleagues to review it. |
Bug Report
Please answer these questions before submitting your issue. Thanks!
1. Minimal reproduce step (Required)
I'm sorry, but I'm not sure how to reproduce the issue. It occurs sporadically on multiple running TiDB processes.
2. What did you expect to see? (Required)
3. What did you see instead (Required)
fatal error on
tidb/pkg/sessionctx/stmtctx/stmtctx.go
Lines 800 to 804 in d13e52e
These errors always occur on the same line.
4. What is your TiDB version? (Required)
The text was updated successfully, but these errors were encountered: