Fix: Clear stale relay mappings and remove config block when disabled#296
Fix: Clear stale relay mappings and remove config block when disabled#296hikmethankolay wants to merge 1 commit intoBillionmail:devfrom
Conversation
This update clears stale relay transport mappings from the database when no active relay configs exist, ensuring fallback to original SMTP. It also removes the relay configuration block from main.cf when relay is disabled, improving cleanup and logging.
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #293 by ensuring proper cleanup when SMTP relay functionality is disabled or deleted. The changes prevent the "mail transport unavailable" error that previously occurred due to stale database entries and orphaned Postfix configuration.
Key changes:
- Clears relay transport mappings from the database when relay system becomes inactive
- Removes the relay configuration block from Postfix's main.cf when relay is disabled
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
| content = content[:beginIndex] + content[blockEnd:] | ||
| modified = true | ||
| g.Log().Info(nil, "Removed relay configuration block from main.cf") |
There was a problem hiding this comment.
Context parameter inconsistency: The logging call uses nil for the context parameter, but the pattern used elsewhere in this file when context is unavailable is context.Background() (see lines 705, 747, 756, 758). Using context.Background() instead of nil is more idiomatic and consistent with the rest of the codebase.
| g.Log().Info(nil, "Removed relay configuration block from main.cf") | |
| g.Log().Info(context.Background(), "Removed relay configuration block from main.cf") |
| // Include trailing newline if present | ||
| if blockEnd < len(content) && (content[blockEnd] == '\n' || content[blockEnd] == '\r') { | ||
| blockEnd++ | ||
| if blockEnd < len(content) && content[blockEnd] == '\n' { |
There was a problem hiding this comment.
Newline handling could be more robust: The logic handles common cases like \n, \n\n, and \r\n, but doesn't consistently handle all newline character combinations. After checking for the first newline character (which could be \n or \r), the second check only looks for \n. Consider checking for any newline character in the second check as well to handle edge cases more consistently.
| if blockEnd < len(content) && content[blockEnd] == '\n' { | |
| if blockEnd < len(content) && (content[blockEnd] == '\n' || content[blockEnd] == '\r') { |
| // Clear stale relay transport mappings to ensure fallback to original SMTP | ||
| _, err = g.DB().Model("bm_domain_smtp_transport").Where("atype", "relay").Delete() | ||
| if err != nil { | ||
| g.Log().Warningf(ctx, "Failed to clear relay transport mappings: %v", err) |
There was a problem hiding this comment.
Error handling inconsistency: Similar database delete operation at line 863 returns an error when the delete fails, but here it only logs a warning. This inconsistency could mask database issues. Consider returning the error instead of just logging it to maintain consistent error handling behavior throughout the function.
| g.Log().Warningf(ctx, "Failed to clear relay transport mappings: %v", err) | |
| return gerror.Wrap(err, "Failed to clear relay transport mappings") |
fixes #293
Purpose
When an SMTP relay is disabled or deleted, the system fails to fall back to the server's own/local SMTP and returns "mail transport unavailable" error. This occurs because:
bm_domain_smtp_transporttable retains relay transport mappings pointing to non-existent SMTP servicesmain.cfrelay configuration block (sender_dependent_default_transport_maps) remains active even after relay is disabledThis PR ensures proper cleanup when relay functionality is disabled, restoring expected fallback behavior.
Implementation
File:
core/internal/service/relay/config_sync.goChange 1 - Clear stale transport mappings (lines 443-447):
Change 2 - Remove relay config block from
main.cf(lines 808-822):Testing
go test)