Hello, I have this exception but the server doesn’...
# ktor
v
Hello, I have this exception but the server doesn’t crash and the sessions works well. java.lang.IllegalStateException: Sessions feature should be installed to use sessions. Do you have an idea why please ?
1
d
Have you a
install(Sessions)
statement?
v
Yes sure : install(Sessions) { header<AnniaSession>(“ANNIA_SESSION”) { transform(SessionTransportTransformerEncrypt(secretEncryptKey, secretAuthKey)) //Use AES and HmacSHA256 } }
And the session works I have just this exception sometimes
d
and you have the install toplevel in the module, or as part of your route? 1️⃣
Copy code
fun Application.module() {
    install(Sessions)
    route("path") {
       get { ... }
    }
}
2️⃣
Copy code
fun Application.module() {
    route("path") {
       install(Sessions)
       get { ... }
    }
}
v
I install it inside main function : fun Application.mainWithParameter(oauthHttpClient: HttpClient) {
Like your first example
d
okay
looks like a bug then or something strange?
v
The weird thing is that works and just sometimes I have this exception but nothing crash
d
and you don’t have anything strange like two applications, or things like that, right?
v
I have just my back with ktor and a route to serve a react application
The exception come always from this route : get(“/isAuthenticated”) { val session = call.getSession() //check authentication if (admins.contains(session.email)) call.respondSuccessJson(“”"{“admin”: true, “email”: “${session.email}“}”“”) else call.respondSuccessJson(“”"{“admin”: false, “email”: “${session.email}“}”“”) }
d
it would be super nice if you could reproduce it in a small example that doesn’t include sensitive code, so we can try ourselves to debug it
v
I will try but as I said this exception appear very randomly 😞
d
well we can always try to stress the server
and put a breakpoint for that exception
v
Ok I will try 🙂
🙏 1
I have just created a small file with the route for login and the other to check if you are logged (this route launch the exception). Where do you want the test file ?
Is it ok for you ?
d
if you can provide your main, or the application.conf, would be great (just in case your specific configuration is important to reproduce the issue). Ideally if you can create a repo or a zip file containing all the required files to run the sample, would be awesome
v
OK no problem 🙂
🙏 1
I simplify the max the code to isolate the problem, I hope you will find it 🙂
d
thanks!
(Without binaries)
v
Ah yes sorry I forgot to remove this
d
no problem 🙂 I just uploaded it without them in the case I need to redownload it again
v
On my ktor server, the exception appear for the route isAuthenticated, thanks for your help !
d
which operating system are you using?
the isAuthenticated route fails when you are sending the token?
refreshing a browser (plain GET requests) in a macOS doesn’t seems to trigger the issue
v
The weird thing is this route works well but sometimes that send the exception and when I looked the log, the exception come from this route
d
which operating system are you using to run the server?
v
I deploy it on clevercloud
d
so a linux machine?
and can you reproduce it locally?
v
For the moment, I didn’t have this exception locally, but the difficult thing is this exception appear just sometimes so I don’t know how to debug it
Yes linux machine
Ok I succeed to reproduce it locally
d
Tried:
gradle run > out.txt
ab -n20000 -c100 -H "ANNIA_SESSION:email=%23stest%40test.test&ttl=%23s180816110048/dc58f97f757566cd943ca87314ec4baa74a1bc38" <http://127.0.0.1:8080/isAuthenticated>
and
ab -n20000 -c100 <http://127.0.0.1:8080/isAuthenticated>
And also an invalid session
no luck from my side
v
The exception appear in my code if I put a random wrong header
d
for example?
v
But with the test version I gave you I don’t have the exception 🤔
I will try to reproduce it in the TestVersion I gave you and I come back, sorry
d
okay, no problem thanks!
v
Ok so if in the install of session you install : transform(SessionTransportTransformerEncrypt(secretEncryptKey, secretAuthKey)) //Use AES and HmacSHA256
And after you put a wrong header and you send the request, the exception appear
d
nice!
🙂
v
So I think the exception is throw from the function transform and after I took this exception with the feature StatusPage
But normally, I think the session just need to be null and don’t throw an exception ?
d
maybe, let me reproduce it
v
Thanks !
d
it should be consistent with other transformers
can you fill a issue at github?
I’ll check it asap
v
Yes sure
🙏 1
I just remember, I already asked this question two months ago, https://github.com/ktorio/ktor/issues/445, and you said me to put a try and catch around the specific line, is it the same problem I think ?
But now the DEBUG message is an ERROR message
d
yeah, I would just try catch the whole method and return null
it can fail for several reasons: including a non hex character
or an invalid header
and so on
v
I will try thanks Carlos ! Sorry for the lost of time
d
not lost at all, let’s fix this 🙂
v
I can’t catch this exception, it go directly to Status feature, I need to do a Snapshot i think
d
I’m going to make a PR to ktor tiself
in the meantime yes, you will have to
use a custom version
v
Ok perfect, thank you very much !
d
v
Do you know when is the next release ?
d
Nope, that’s not usually predictable, if you are in a rush, you will have to use a custom version depending on your requirements. But if only happen to invalid sessions, maybe you can just wait and ignore those errors?
v
yes that’s what I will do 😉 Thanks