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

Bugfix - Handling blocked users gracefully when reacting to their messages #412

Merged
merged 6 commits into from
Mar 15, 2022

Conversation

interacsion
Copy link
Contributor

warped the line that reacts to the message in a try catch block. see file changes.
resolves #406.

@interacsion interacsion added bug Something isn't working enhancement New feature or request labels Mar 14, 2022
@interacsion interacsion requested a review from a team as a code owner March 14, 2022 14:37
@interacsion interacsion self-assigned this Mar 14, 2022
@interacsion interacsion requested a review from a team as a code owner March 14, 2022 14:37
@interacsion
Copy link
Contributor Author

Why complete

can't catch queue(), afaik.

@interacsion
Copy link
Contributor Author

interacsion commented Mar 14, 2022

iirc, queue() throws exceptions on the executing thread, thus you can't catch them on the calling thread.
correct me if I am wrong

@Tais993
Copy link
Member

Tais993 commented Mar 14, 2022

queue is async indeed, so a try-catch doesn't work.
Instead you have to use the consumers, take a look at the documentation here.

@interacsion
Copy link
Contributor Author

.queue(ignored -> {},
exception -> {
    if (exception instanceof ErrorResponseException && ((ErrorResponseException) exception).getErrorResponse() == ErrorResponse.REACTION_BLOCKED) {
        return;
    }
    exception.printStackTrace();
});

is this better?

@Zabuzard
Copy link
Member

Zabuzard commented Mar 14, 2022

u have to use our logging system, not e.printStackTrace. so at least logger.error("...", e)

@interacsion
Copy link
Contributor Author

interacsion commented Mar 14, 2022

The best thing to do is to throw it, probably, but how do I do it from inside the function? 🤔

I think staying with .complete() and try catch is the best solution. I think we shouldn't handle other exceptions here.

@Zabuzard
Copy link
Member

Zabuzard commented Mar 14, 2022

The best thing to do is to throw it, probably, but how do I do it from inside the function? 🤔

Then you would throw from within the JDA context where the lambda is executed, which is detached from our flow - not good.

If you stick with queue, you should log the error using the logger.

If you stick with complete, try-catch is fine. That means that the call is blocking though and not async. Which means that you could potentially block one of our bot-core-system threads for a long time. We only have a few of these, so if there are many reactions going on and they, for whatever reason, take long to complete, that can block the whole bot and stuff like /tag foo can not be executed anymore and will die, cause they will want to use the same threads, which are all blocked by the slow reaction.

And all of that just because you want to throw an exception, you do not even have business logic or anything that requires a blocking call. Thats not really worth it.

tldr, dont block our threads, use queue.

(in this particular case it would actually be safe though, since as of today, BotCore doesnt manage EventListeners itself but just forward them to JDA directly - so they dont share an executorservice with slash commands)

@interacsion
Copy link
Contributor Author

that should do it.

@interacsion
Copy link
Contributor Author

When it logs an exception the stack trace shows this.

image

is that supposed to happen? it doesn't happen if I don't use any error handler for the queue().

@Tais993
Copy link
Member

Tais993 commented Mar 15, 2022

I don't know or it should, please give the full stacktrace

@interacsion
Copy link
Contributor Author

@Zabuzard
Copy link
Member

Sounds like you logged the error now. Didnt you want to ignore it and only log unknown failures? Whats the code that produced this?

@interacsion
Copy link
Contributor Author

I commented the part that ignores the error, for the sake of seeing a stacktrace, as I couldn't think of any other way to make it fail.

@interacsion
Copy link
Contributor Author

interacsion commented Mar 15, 2022

I found another way to make it fail.
https://gist.github.com/MaiTheLord/99335c4206d4f81da055df4fbb5e6deb
produced by latest code, 8d554a3, and revoking the Add Reactions permission

@Zabuzard
Copy link
Member

Zabuzard commented Mar 15, 2022

Not sure what your question or confusion is, to be honest. This is the expected behavior - it logs the unknown error. Looks good to me 👍

@interacsion
Copy link
Contributor Author

I wasn't sure about the caused by ContextException.

when I look at it now, seems like its JDA's way to handle async exceptions.

@Tais993
Copy link
Member

Tais993 commented Mar 15, 2022

When it logs an exception the stack trace shows this.

ah yeah, that's just the exception, scroll up and you see the cause.

I found another way to make it fail.
https://gist.github.com/MaiTheLord/99335c4206d4f81da055df4fbb5e6deb
produced by latest code, 8d554a3, and revoking the Add Reactions permission

There's a ton of ways to make it fail, but we shouldn't do anything with this.
It logs the error, the bot owner can add permissions, not our issue.
Just keep it like this

interacsion and others added 2 commits March 15, 2022 10:07
Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com>
Tais993
Tais993 previously approved these changes Mar 15, 2022
@interacsion
Copy link
Contributor Author

Zabuzard, can you review please?

@sonarcloud
Copy link

sonarcloud bot commented Mar 15, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@Zabuzard Zabuzard enabled auto-merge (squash) March 15, 2022 09:07
@Zabuzard Zabuzard merged commit eecb610 into develop Mar 15, 2022
@Zabuzard Zabuzard deleted the bugfix/handling_blocked_users_when_reacting branch March 15, 2022 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autoreact on suggestions doesnt handle blocked users gracefully
3 participants