wangchenxuya commented on code in PR #1160: URL: https://github.com/apache/incubator-seata-go/pull/1160#discussion_r3925335162
########## pkg/tm/transaction/endphase/endphase.go: ########## @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package endphase normalizes the outcome of a global transaction second +// phase so that every transport classifies the same situation the same way. +package endphase + +import ( + "context" + "errors" + "fmt" +) + +// ErrEmptyResponse reports a second phase that finished without a transport +// error but produced no response for the caller to read a status from. +var ErrEmptyResponse = errors.New("empty second phase response") + +// Err turns the outcome of a commit or rollback retry loop into a single error +// value, and returns nil only when res holds a response the caller can use. +// +// The retry loops stop as soon as the backoff is no longer Ongoing, which also +// happens when the caller's context is already done before the first attempt. +// No request is sent in that case and sendErr stays nil, so callers must never +// treat a nil sendErr on its own as success. +// +// Checks run in a fixed order: +// +// 1. a response that came back, which outranks everything else +// 2. the caller's context, so a canceled or expired context is reported as +// such and stays matchable with errors.Is +// 3. the last transport error, annotated with why the retries stopped +// 4. the backoff terminating on its own, that is, retries exhausted +// 5. a missing response +func Err(ctx context.Context, sendErr, backoffErr error, res interface{}) error { + // A response outranks a done context. The transports send without a + // context, so the retry loop can outlive the deadline while the request + // itself still reached the TC and came back. Reporting a failure here + // would hide a second phase that actually completed. + if sendErr == nil && res != nil { Review Comment: Confirmed, thanks — the gap is a bit wider than a corrupt payload, too. - The getty byte readers drop the short-read error and return zero (`value, _ := buf.ReadByte()`), so a truncated/corrupt body does decode into a non-nil zero-value response, as you said. - `ResultCodeFailed` is the zero value, so that zero-value response carries `ResultCodeFailed`. - Neither `Commit` nor `Rollback` ever looked at `ResultCode` — only `Begin` does. So the same gap also let an explicit TC failure through as a success, not just a corrupt payload. Pushed a fix: all four paths now check `ResultCode` the way `Begin` already does, via `endphase.RejectedResponse`. Verified against a real TC that a successful commit/rollback carries `ResultCodeSuccess`, so this doesn't reject healthy traffic. Four existing test fixtures had left `ResultCode` unset (zero value) and started failing once the check went in — that was the check catching them, and they've been updated. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
