Skip to content

Conversation

@cleitonme
Copy link
Contributor

2025-11-01 09:30:41 -03:00 INFO Already logged in, just connect role=wuzapi
panic: couldn't dial whatsapp web websocket: dial tcp 127.0.0.1:5983: connect: connection refused

goroutine 90 [running]:
main.(*server).startClient(0xc00040c980, {0xc0003ba000, 0x20}, {0xc0003ba020, 0x1f}, {0xc0002b6030, 0x24}, {0xc00052af00, 0xb, 0x10})
/home/deploy/WebstormProjects/wuzapi/wmiau.go:537 +0x104d
created by main.(*server).routes.(*server).Connect.func15 in goroutine 86
/home/deploy/WebstormProjects/wuzapi/handlers.go:265 +0x679

Removed panic(err): Replaced with proper error handling that:

Logs the error in detail
Cleans resources (clients and connections)
Updates the status in the database
Sends a webhook notifying of the error
Returns normally without crashing the application

Proxy error handling: The code already had good handling for invalid URLs, but now with clearer logs
Removed another panic: The panic after GetDevice was also replaced by the creation of a new device

Benefits:

✅ The application no longer crashes when there is a connection error ✅ The user is notified via webhook about the error ✅ The status in the database is updated correctly
✅ Resources are cleaned properly
✅ Detailed logs for debugging

Removed panic(err): Replaced with proper error handling that:

Logs the error in detail
Cleans resources (clients and connections)
Updates the status in the database
Sends a webhook notifying of the error
Returns normally without crashing the application

Proxy error handling: The code already had good handling for invalid URLs, but now with clearer logs
Removed another panic: The panic after GetDevice was also replaced by the creation of a new device

Benefits:

✅ The application no longer crashes when there is a connection error
✅ The user is notified via webhook about the error
✅ The status in the database is updated correctly
✅ Resources are cleaned properly
✅ Detailed logs for debugging
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @cleitonme, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the application's stability and error handling capabilities. By replacing panic calls with structured error management, the system is now more resilient to connection problems and device retrieval issues. It ensures that failures are gracefully handled, resources are properly managed, and relevant stakeholders are notified, leading to a more robust and reliable user experience.

Highlights

  • Eliminated Application Crashes: Replaced critical panic(err) calls with robust error handling mechanisms, ensuring the application no longer crashes due to connection failures or issues retrieving device information.
  • Enhanced Connection Stability: Implemented a retry logic with exponential backoff for client connection attempts, improving resilience against transient network issues.
  • Improved Error Feedback and Resource Management: Upon connection failure, the system now logs detailed errors, cleans up associated client resources, updates the user's connection status in the database, and sends a webhook notification, providing comprehensive feedback and maintaining system integrity.
  • Clearer Proxy Configuration Logs: Added informational logging to indicate successful proxy configuration for both SOCKS and HTTP/HTTPS proxies, aiding in debugging and operational visibility.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively removes several panic calls, which will improve the application's stability by preventing crashes on connection errors. The introduction of a retry mechanism with backoff for connection attempts is a great addition for resilience. I've added a few comments with suggestions to improve code clarity and maintainability, such as removing duplicated code, correcting a misleading comment about the backoff strategy, and using constants for magic numbers.

wmiau.go Outdated
Comment on lines 400 to 405
jid, _ := parseJID(textjid)
deviceStore, err = container.GetDevice(context.Background(), jid)
if err != nil {
panic(err)
log.Error().Err(err).Msg("Failed to get device")
deviceStore = container.NewDevice()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code (lines 399-409) appears to be a duplicate of an earlier block (lines 370-380). The deviceStore is re-initialized here, but this new value is not used, as the whatsmeow.Client was already created on lines 392/394 with the deviceStore from the first block. This redundant code can be confusing and should be removed to improve maintainability.

wmiau.go Outdated
if err != nil {
panic(err)

// Retry logic com backoff exponencial
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This comment is not in English and incorrectly describes the backoff strategy as exponential. The implementation time.Duration(attempt) * 5 * time.Second results in a linear backoff (5s, 10s, ...). Please update the comment to be in English and accurately reflect the linear backoff.

Suggested change
// Retry logic com backoff exponencial
// Retry logic with linear backoff

wmiau.go Outdated
Comment on lines 561 to 566
maxRetries := 3
var lastErr error

for attempt := 0; attempt < maxRetries; attempt++ {
if attempt > 0 {
waitTime := time.Duration(attempt) * 5 * time.Second
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The values 3 for max retries and 5 * time.Second for the backoff interval are hardcoded. It's a good practice to define these 'magic numbers' as named constants (e.g., maxConnectionRetries and connectionRetryBaseWait) at the top of the function or file. This improves readability and makes the code easier to maintain and configure.

wmiau.go Outdated
log.Error().Err(dbErr).Msg("Failed to update user status after connection error")
}

mycli := MyClient{client, 1, userID, token, subscriptions, s.db, s}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The mycli variable is already defined in the outer scope of this function (on line 415). Re-creating an identical instance here is redundant. You can remove this line, and the sendEventWithWebHook call on line 615 will use the existing mycli instance from the outer scope.

@cleitonme
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a great improvement for the application's stability. Replacing the panic calls with proper error handling, especially for connection failures, is a critical fix. The introduction of a retry mechanism with backoff for connection attempts is well-implemented and significantly improves the resilience of the service. The cleanup of resources, database updates, and webhook notifications on failure are all excellent additions.

I have one suggestion to further improve the retry logic by using an exponential backoff strategy, which is generally more robust for such scenarios.

Overall, this is a solid contribution that directly addresses the reported crashes.


for attempt := 0; attempt < maxConnectionRetries; attempt++ {
if attempt > 0 {
waitTime := time.Duration(attempt) * connectionRetryBaseWait
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the current linear backoff is a good start, using an exponential backoff is generally a more robust strategy for connection retries. It increases the wait time more significantly with each attempt, which can be more effective during longer service disruptions.

With maxConnectionRetries set to 3, the sequence of wait times is the same as with linear backoff. However, this change implements a true exponential backoff, making the logic more scalable and robust if maxConnectionRetries is increased in the future.

For even greater robustness against 'thundering herd' problems, you might also consider adding a small random jitter to the wait time in a future enhancement.

Suggested change
waitTime := time.Duration(attempt) * connectionRetryBaseWait
waitTime := connectionRetryBaseWait * time.Duration(1<<(uint(attempt)-1))

@asternic asternic merged commit 17b5a1d into asternic:main Nov 3, 2025
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.

2 participants