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

500 Internal Server Error on SetUp #2704

Open
RowMax03 opened this issue Nov 29, 2024 · 46 comments
Open

500 Internal Server Error on SetUp #2704

RowMax03 opened this issue Nov 29, 2024 · 46 comments

Comments

@RowMax03
Copy link

RowMax03 commented Nov 29, 2024

IMPORTANT: Please search the issues, including closed issues, and the FAQ before opening a new issue. The template is mandatory; failure to use it will result in issue closure.

Describe the bug

So i tried setting up the integration for ca 40mins now. Dosen't matter if I choose amazon.de or .com use Otp or not after the Alexa sign in screen pops up. It either says that my account dosen't exist or it will give me Captcha challange which will result in a 500 Server Error.

To Reproduce

  1. Go to 'Install'
  2. Click on 'Submit'
    Try to Sign in into Amazon Alex Account
  3. See error

Expected behavior

Sign in/Account Linking working

System details

  • Home Assistant version:
    Core
    2024.11.3
    Frontend
    20241106.2
  • alexa_media version (from const.py or HA startup log):
    5.0.0
  • alexapy version (from pip show alexapy in homeasssistant container or HA startup log):
  • Is Amazon 2FA/2SV enabled <!---We will not debug login issues if unanswered---> (y/n): y, tried first without and then with on .com and .de
  • Amazon Domain: .com/.de

Debug Logs (alexa_media & alexap
home-assistant_2024-11-29T01-52-26.043Z.log
y)

Please provide [logs]Additional context

It could be the case that the Amazon Servers are currently not working correctly because its 2:30 in the night

@MaJerle
Copy link

MaJerle commented Nov 29, 2024

Same issues here.

@samsonusmc
Copy link

Same and following

@Kospolo
Copy link

Kospolo commented Nov 30, 2024

Same here. Following.

@Reecem98
Copy link

Same issue. Following

@malosaaa
Copy link

After setting up same error as you guys here the log

Logger: homeassistant.util.package
Source: util/package.py:152
First occurred: 5:35:22 AM (3 occurrences)
Last logged: 5:35:27 AM

Unable to install package alexapy==1.29.4: × No solution found when resolving dependencies: ╰─▶ Because alexapy==1.29.4 depends on aiofiles>=23.1.0,<24.0.0 and aiofiles>=24.1.0, we can conclude that alexapy==1.29.4 cannot be used. And because you require alexapy==1.29.4, we can conclude that your requirements are unsatisfiable.

@hendmele
Copy link

same here

@Fullmojjo
Copy link

Same here

@danielbrunt57
Copy link
Collaborator

After setting up same error as you guys here the log

Logger: homeassistant.util.package
Source: util/package.py:152
First occurred: 5:35:22 AM (3 occurrences)
Last logged: 5:35:27 AM

Unable to install package alexapy==1.29.4: × No solution found when resolving dependencies: ╰─▶ Because alexapy==1.29.4 depends on aiofiles>=23.1.0,<24.0.0 and aiofiles>=24.1.0, we can conclude that alexapy==1.29.4 cannot be used. And because you require alexapy==1.29.4, we can conclude that your requirements are unsatisfiable.

alexapy 1.29.4 does not support HA Core 2024.12 yet.
Core 2024.12 updated Python from 3.12 to 3.13 and aiofiles from 23.1.2 to 24.1.0
Currently, alexapy requires aiofiles 23.x...

@MaJerle
Copy link

MaJerle commented Nov 30, 2024

@danielbrunt57 The OP has version 2024.11.

Home Assistant version:
Core
2024.11.3

@danielbrunt57
Copy link
Collaborator

@danielbrunt57 The OP has version 2024.11.

Home Assistant version:
Core
2024.11.3

I realize that but I was not replying to the op but @malosaaa, who should not have posted his error here as his error is due to HA Core 2024.12.0b# where python is now 3.13 and aiofiles is 24.1.0.

@randywgreen
Copy link

User error for me... I was using the wrong email/password combination on the configuration UI causing it to result in a 500 error as to be expected.

@danielbrunt57
Copy link
Collaborator

User error for me... I was using the wrong email/password combination on the configuration UI causing it to result in a 500 error as to be expected.

Otherwise known in IT support as a PEBKAC issue - Problem Exists Between Keyboard And Chair! 😊

@thefunkygibbon
Copy link

sigh, literally every single time i got to install this bloody plugin it seems to be broken in some way.

@RowMax03
Copy link
Author

RowMax03 commented Dec 2, 2024

Tp be fair the issue is likely on Amazon. During Setup Amazon was telling me that the account I am using isn't signed up (It is, I used it in the Alexa app to doublecheck and the signup process it was defaulting to was telling me that the email was already used). Sadly however it's on the devs of this plugin to fix it or give clearer instructions for eu users maybe?

@xyloidpeople
Copy link

I am also having:
"Config flow could not be loaded: 500 Internal Server Error Server got itself in trouble"
which I find to be a very poor error message.
This is a very unreliable integration. It regularly does not work and throws my setup into kaos. I use TTS on my alexa devices to alert us during day to day operation. I hope they fix this finally.

I also use 2FA so that is not the problem. An I am current on ALL updates.

@danielbrunt57
Copy link
Collaborator

sigh, literally every single time i got to install this bloody plugin it seems to be broken in some way.

@thefunkygibbon Comments like these unfortunately do not help contributors want to solve issues and for me, only serve to make me want to just fork this project for my own personal and private use. I really should not be wasting my precious time trying to help fix things for others when my setup here in Canada is working perfectly fine with the code the way it is. Complaints and negative comments turn me off.

@danielbrunt57
Copy link
Collaborator

Tp be fair the issue is likely on Amazon. During Setup Amazon was telling me that the account I am using isn't signed up (It is, I used it in the Alexa app to doublecheck and the signup process it was defaulting to was telling me that the email was already used). Sadly however it's on the devs of this plugin to fix it or give clearer instructions for eu users maybe?

I am thinking that is probably accurate. It's a known fact that Amazon did change the Alexa login windows earlier this year. The Alexa app used to present email + password together on first screen then optionally followed by OTP input if configured on the account. The login sequence now is 1. email, 2. password, optionally 3. OTP input. Another developer found a different URL for us to use which starts with sign in as new account and we have to select sign in to existing account where email + password are together followed by OTP input. I've dug inside the code for alexa-remote2 (javascript) from which this integration was developed years ago and have asked a few questions to the author of that repo who still actively works on it. Unfortunately, AMP was hardcoded to make things more user friendly for us than the way remote2 gets kickstarted, which is basically immune to authentication processes by Amazon. It just informs you to manually open a URL in your browser. that URL is a simply proxy callback when authentication is successful. It just waits for the success message however long and however many hoops Amazon takes you through (OTP, CAPTCHA, double verify you are who you say you are via an OTP SMS to the phone number on the account, open the Alexa app and verify yourself, or, or, or. Once success is achieved, the callback retrieves the login session cookie to kickstart itself. Yes! I've spent many MANY sleepless nights trying to figure this thing out, and I only recently started this deep-dive inside Python and the huge learning curve 5 months ago. Before then, I really had no understanding of the inside workings of "HA", and all in my "spare" time when not working, socializing, watching TV, sleeping. Okay...!
I want to try and figure out how to get back to the basic auth process like alexa-remote2.. Thank god for "Google: and "ChatGPT"!

Hopefully, we'll get there. Please have patience even when you're frustrated!

One more comment, yesterday Apollon77 (alexa-remote2) did point me to his code section where he creates the Alexa "login" URL. I just need to check if that's the URL we're using or not. My first impression when I looked at it is I think it might be the URL we used to use 4 months ago and if so, doesn't help me provide an instant fix but will serve as a starting point to develop a new method of authenticating AMP with Amazon (Alexa). And that will take time unless other contributors get involved.

@danielbrunt57
Copy link
Collaborator

danielbrunt57 commented Dec 3, 2024

I am also having:
"Config flow could not be loaded: 500 Internal Server Error Server got itself in trouble"
which I find to be a very poor error message.

Unfortunately, all that AMP sees is "HTML error 500" which means....

Google:
When you encounter an error 500, it means that the server has encountered an unexpected condition or configuration problem that prevents it from fulfilling the request made by the browser or client. The HTTP status code “500 – Internal Server Error” is one of the many 5.X.X. HTTP error codes (500, 502, 503, 504, etc.).

If you'd rather see ""Config flow could not be loaded: 500 Internal Server Error: The server has encountered an unexpected condition or configuration problem that prevents it from fulfilling the request made by the browser or client", I can make that happen! I can probably also add additional details of the code section that it occurred in. But none of that will help me solve the reason it occurred.

@malosaaa
Copy link

malosaaa commented Dec 3, 2024

@danielbrunt57 The OP has version 2024.11.

Home Assistant version:
Core
2024.11.3

I realize that but I was not replying to the op but @malosaaa, who should not have posted his error here as his error is due to HA Core 2024.12.0b# where python is now 3.13 and aiofiles is 24.1.0.

You right, i totally forget i was testing it on my beta system... silly me... well i wait till its supported then

@MaJerle
Copy link

MaJerle commented Dec 3, 2024

sigh, literally every single time i got to install this bloody plugin it seems to be broken in some way.

I guess you are welcome to buy NabuCasa cloud and use native integration. You aren't really helping here.

@danielbrunt57
Copy link
Collaborator

danielbrunt57 commented Dec 3, 2024

I guess you are welcome to buy NabuCasa cloud and use native integration.

"I guess you are welcome to buy subscribe to NabuCasa cloud and use native integration"

That semantic aside 😊, Nabu Casa Cloud only provides TTS to Echo devices and nothing else that AMP can do such as Actionable Notifications, integration of Echo connected devices like Zigbee via Echo hub, temperature sensors, AQM sensors, etc. into HA.
But if you only use or want TTS and nothing more, NCC is trouble free and works for what it provides.
Additionally, with NCC you get a UI driven method of selecting/deselecting HA entities to present to Alexa for discovery and control rather than navigating the technically complex setup of your own Alexa Home Assistant Skill in Amazon. FYI, this aspect can now be accomplished quite easily via the new Home-Assistant-Matter-Hub custom Add-on for HA!

The only reasons I stick with AMP are Actionable Notifications and last_device!

@MaJerle
Copy link

MaJerle commented Dec 3, 2024

it is for free, dont complain. And few bucks/month for HA isnt a lot either.

@thefunkygibbon
Copy link

@danielbrunt57 @MaJerle - Can someone not vent frustration? I feel that it isn't particularly out of order, all things considered. I have attempted to get the integration to work probably 4 or 5 times in the last couple of years and each time it didn't work for some issue with not supporting the latest code changes of HA or amazon changing things too which no doubt causes issues. but from a user perspective, and considering that this integration caused major HA bootloop problems only a few months back, it's not ideal.
sorry you got offended. I'm more than happy to help troubleshoot things, because I would certainly like to get this working now i'm setting up voice pipeline stuff.
also subscribing to nabucasa is overkill , i didnt even think it gave the ability to do what this integration does. I thought it was more to do with being able to control HA stuff with Alexa.
not providing ways of using alexa as a media device. either way, as it would be literally only needed for that because all of the other nabucasa aspects I already have in place already. thanks

@thefunkygibbon
Copy link

thefunkygibbon commented Dec 3, 2024

"It's free, don't complain."

So you're saying that no one should be frustrated with something that claims to work but has issues, just because it's free? That’s an odd thing to suggest. Home Assistant, like many free services, sometimes has problems, and it’s completely reasonable to express disappointment when things don’t work as expected.

Just to clarify, my comment was based on my personal experience and opinion. I wasn't making any broad claims or complaining about support or feeling entitled. I was simply stating what I’ve encountered. If you’re unsure, maybe give my comment another read.

"And a few bucks/month for HA isn't a lot either."

As I’ve pointed out, paying 75 euros a year for features I don’t even need—like remote access and Alexa integration—doesn’t seem worth it. I already have those capabilities. Plus, the subscription service doesn’t even offer the ability to stream audio to Alexa, anyway, at least according to the Nabucasa documentation I’ve come across.

@MaJerle
Copy link

MaJerle commented Dec 3, 2024

The only reason I stick with AMP is Actionable Notifications!

I don't know how you call it, but I like AMP to be clear, and in parallel I use the NabuCasa cloud, because I can expose entities and control them with voice (VTC = Voice To Command? :))

My point was and still is that if you get something for free (and especially software) you should never expect to be perfect. Even paid software solution has "only" 99.9% uptime. If you pay, you have absolutely all the means to complain and go nuts.

Sadly, free software is often free for a reason. We should be thankful for having HA 100% FOC and same for this great extension, that is currently sadly not working. But it will come back soon.

Then, I'd like to see thank you from all of you ;)

@danielbrunt57
Copy link
Collaborator

sigh, literally every single time i got to install this bloody plugin it seems to be broken in some way.

@thefunkygibbon

Let's keep things positive here!
Posting the following categories of comments is not helpful and serves no useful, positive purpose.

  • Complaint: This is the most straightforward term, indicating a statement of dissatisfaction.
  • Gripe: This suggests a persistent complaint, often about a minor annoyance.
  • Whine: This implies a complaining tone, often seen as excessive or petty.
  • Rant: This indicates a passionate and often angry expression of complaint.
  • Venting frustration
  • Expressing dissatisfaction
  • Complaining about a recurring issue

If you need to vent the above thoughts, try the wife or your friends! 😉

@danielbrunt57
Copy link
Collaborator

So you're saying that no one should be frustrated with something that claims to work but has issues, just because it's free?

No, that's not what we're saying.

That’s an odd thing to suggest. Home Assistant, like many free services, sometimes has problems, and it’s completely reasonable to express disappointment when things don’t work as expected.

It's perfectly reasonable to express your disappointments, but not here. GitHub issues is a place to report issues, get help, offer suggestions, assistance or recommendations, etc. but not complaints, gripes, whining or venting, etc.

If you want to continue discussing this, feel free to pm me over in HA community forum (dbrunt).

@danielbrunt57
Copy link
Collaborator

I don't know how you call it, but I like AMP to be clear, and in parallel I use the NabuCasa cloud, because I can expose entities

You can now do that for free with the Home Assistant Matter Hub add-on. The means of selecting/deselecting exposed entities is not point and click like nabu but it is easier than the DIY smart home skill.

and control them with voice (_VTC = Voice To Command?)

The acronym is TTS (text to speech)

@scottiecb19
Copy link

No complaints here, a 👍 for your efforts creating this. Regrettably I still have the reported issue, tried the latest version and rolled back to another version recommended on a discussion thread. I've not got 2FA set up on amazon, could this be the issue.

@danielbrunt57
Copy link
Collaborator

rolled back to another version recommended on a discussion thread

Which version did you roll back to where it works?

@scottiecb19
Copy link

Which version did you roll back to where it works?

Apologies, my post was a bit misleading. I rolled back to 4.12.7 and 4.12.12 and neither worked. Essentially I haven't got it working, but would like to as I'd like to add voice responses to some of my automations for security reasons.

@xyloidpeople
Copy link

I am not complaining. I am stating a fact. A software package free or not should be tested prior to release. If an error is found after release, then there should be some way to rewind the version to a working state. Should that not be possible, then a news release would be helpful, and hopefully a patch. Most of us use the Alexa integration to give TTS voice alerts about system status within our Home Assistant enviornment.

Error messages should include as much information as possible not "got into trouble".

My Home Assistant is now crippled without the messaging that I have worked on and relied on for the past several years. I am hoping that the developers are able to resolve this issue.

This is a breaking change for me, meaning I may break out the google and replace the alexa.

This is only my opinion so go ahead and flame me.

@samsonusmc
Copy link

I am not complaining. I am stating a fact. A software package free or not should be tested prior to release. If an error is found after release, then there should be some way to rewind the version to a working state. Should that not be possible, then a news release would be helpful, and hopefully a patch. Most of us use the Alexa integration to give TTS voice alerts about system status within our Home Assistant enviornment.

Error messages should include as much information as possible not "got into trouble".

My Home Assistant is now crippled without the messaging that I have worked on and relied on for the past several years. I am hoping that the developers are able to resolve this issue.

This is a breaking change for me, meaning I may break out the google and replace the alexa.

This is only my opinion so go ahead and flame me.

I think when interacting with engineers, providing information is important. Obviously the quality of the information is objective (not subjective).
Objectively, saying it's broken is useful, but saying it's broken and here is the error and here is the log (with for me is: Unable to install package alexapy==1.29.4: × No solution found when resolving dependencies: ╰─▶ Because alexapy==1.29.4 depends on aiofiles>=23.1.0,<24.0.0 and aiofiles>=24.1.0, we can conclude that alexapy==1.29.4 cannot be used. And because you require alexapy==1.29.4, we can conclude that your requirements are unsatisfiable.) is objectively much more helpful.
telling anyone who is doing anything for free is doing it wrong when you lack either the drive or the capability to do it yourself is a slippery slope in any situation.
Especially when...
The break in this case had NOTHING to do with the developer. it was a change to requirements that in in conflict with another component that HA uses and in other examples, Amazon changing.
You have to remember all of this is UNSUPPORTED by Amazon and anything that works is b/c of AMAZING engineering talent to work around the mechanisms Amazon has put in place to try to make these integrations impossible.

@samsonusmc
Copy link

The good news is I see folks working on the PRs (fixes) for alexapy and hopefully we can get a merge and deploy soon.
https://gitlab.com/keatontaylor/alexapy/-/merge_requests

@danielbrunt57
Copy link
Collaborator

A software package free or not should be tested prior to release

I always test my pull requests...in Canada so...move to Canada!!! Please, think before you speak!!!

@danielbrunt57
Copy link
Collaborator

I am not complaining. I am stating a fact. A software package free or not should be tested prior to release.

Fact??? No, you are demanding...

@louisostrowski
Copy link

After setting up same error as you guys here the log

Logger: homeassistant.util.package
Source: util/package.py:152
First occurred: 5:35:22 AM (3 occurrences)
Last logged: 5:35:27 AM

Unable to install package alexapy==1.29.4: × No solution found when resolving dependencies: ╰─▶ Because alexapy==1.29.4 depends on aiofiles>=23.1.0,<24.0.0 and aiofiles>=24.1.0, we can conclude that alexapy==1.29.4 cannot be used. And because you require alexapy==1.29.4, we can conclude that your requirements are unsatisfiable.

alexapy 1.29.4 does not support HA Core 2024.12 yet. Core 2024.12 updated Python from 3.12 to 3.13 and aiofiles from 23.1.2 to 24.1.0 Currently, alexapy requires aiofiles 23.x...

Daniel, any timeline you knownof for when alexapy dependency updates will be bumped up?

@louisostrowski
Copy link

BTW, was using before conversion to UI setup(when YAML setup needed) and it is a amazing add to HA. Ty Daniel!

After setting up same error as you guys here the log

Logger: homeassistant.util.package
Source: util/package.py:152
First occurred: 5:35:22 AM (3 occurrences)
Last logged: 5:35:27 AM

Unable to install package alexapy==1.29.4: × No solution found when resolving dependencies: ╰─▶ Because alexapy==1.29.4 depends on aiofiles>=23.1.0,<24.0.0 and aiofiles>=24.1.0, we can conclude that alexapy==1.29.4 cannot be used. And because you require alexapy==1.29.4, we can conclude that your requirements are unsatisfiable.

alexapy 1.29.4 does not support HA Core 2024.12 yet. Core 2024.12 updated Python from 3.12 to 3.13 and aiofiles from 23.1.2 to 24.1.0 Currently, alexapy requires aiofiles 23.x...

Daniel, any timeline you knownof for when alexapy dependency updates will be bumped up?

@MaJerle
Copy link

MaJerle commented Dec 5, 2024

New release 5.0.1 has been published. We are back online, fellas!

Thanks to all for contribution.

@nkk67
Copy link

nkk67 commented Dec 6, 2024

Deleted everything and reinstalled. After this everything works fine. Thank you

@jgontrum
Copy link

jgontrum commented Dec 7, 2024

Still seeing the 500 status page on setup, even though I'm running 5.0.1 and HA 2024.12.1.
However, this looks like a connection issue to me?

homeassistant  | Traceback (most recent call last):
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_transports/default.py", line 72, in map_httpcore_exceptions
homeassistant  |     yield
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_transports/default.py", line 377, in handle_async_request
homeassistant  |     resp = await self._pool.handle_async_request(req)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpcore/_async/connection_pool.py", line 216, in handle_async_request
homeassistant  |     raise exc from None
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpcore/_async/connection_pool.py", line 196, in handle_async_request
homeassistant  |     response = await connection.handle_async_request(
homeassistant  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |         pool_request.request
homeassistant  |         ^^^^^^^^^^^^^^^^^^^^
homeassistant  |     )
homeassistant  |     ^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpcore/_async/connection.py", line 101, in handle_async_request
homeassistant  |     return await self._connection.handle_async_request(request)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpcore/_async/http11.py", line 143, in handle_async_request
homeassistant  |     raise exc
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpcore/_async/http11.py", line 113, in handle_async_request
homeassistant  |     ) = await self._receive_response_headers(**kwargs)
homeassistant  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpcore/_async/http11.py", line 186, in _receive_response_headers
homeassistant  |     event = await self._receive_event(timeout=timeout)
homeassistant  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpcore/_async/http11.py", line 224, in _receive_event
homeassistant  |     data = await self._network_stream.read(
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |         self.READ_NUM_BYTES, timeout=timeout
homeassistant  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |     )
homeassistant  |     ^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpcore/_backends/anyio.py", line 32, in read
homeassistant  |     with map_exceptions(exc_map):
homeassistant  |          ~~~~~~~~~~~~~~^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/contextlib.py", line 162, in __exit__
homeassistant  |     self.gen.throw(value)
homeassistant  |     ~~~~~~~~~~~~~~^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
homeassistant  |     raise to_exc(exc) from exc
homeassistant  | httpcore.ReadTimeout
homeassistant  | 
homeassistant  | The above exception was the direct cause of the following exception:
homeassistant  | 
homeassistant  | Traceback (most recent call last):
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/aiohttp/web_protocol.py", line 480, in _handle_request
homeassistant  |     resp = await request_handler(request)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/aiohttp/web_app.py", line 569, in _handle
homeassistant  |     return await handler(request)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/aiohttp/web_middlewares.py", line 117, in impl
homeassistant  |     return await handler(request)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 92, in security_filter_middleware
homeassistant  |     return await handler(request)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 83, in forwarded_middleware
homeassistant  |     return await handler(request)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 26, in request_context_middleware
homeassistant  |     return await handler(request)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 86, in ban_middleware
homeassistant  |     return await handler(request)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 242, in auth_middleware
homeassistant  |     return await handler(request)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/src/homeassistant/homeassistant/components/http/headers.py", line 32, in headers_middleware
homeassistant  |     response = await handler(request)
homeassistant  |                ^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/src/homeassistant/homeassistant/helpers/http.py", line 73, in handle
homeassistant  |     result = await handler(request, **request.match_info)
homeassistant  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/config/custom_components/alexa_media/config_flow.py", line 1049, in wrapped
homeassistant  |     return await cls.handler(request, **kwargs)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/authcaptureproxy/auth_capture_proxy.py", line 442, in all_handler
homeassistant  |     resp = await getattr(self.session, method)(
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |         site, data=data, headers=headers, follow_redirects=True
homeassistant  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |     )
homeassistant  |     ^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_client.py", line 1905, in post
homeassistant  |     return await self.request(
homeassistant  |            ^^^^^^^^^^^^^^^^^^^
homeassistant  |     ...<13 lines>...
homeassistant  |     )
homeassistant  |     ^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_client.py", line 1585, in request
homeassistant  |     return await self.send(request, auth=auth, follow_redirects=follow_redirects)
homeassistant  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_client.py", line 1674, in send
homeassistant  |     response = await self._send_handling_auth(
homeassistant  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |     ...<4 lines>...
homeassistant  |     )
homeassistant  |     ^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_client.py", line 1702, in _send_handling_auth
homeassistant  |     response = await self._send_handling_redirects(
homeassistant  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |     ...<3 lines>...
homeassistant  |     )
homeassistant  |     ^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_client.py", line 1739, in _send_handling_redirects
homeassistant  |     response = await self._send_single_request(request)
homeassistant  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_client.py", line 1776, in _send_single_request
homeassistant  |     response = await transport.handle_async_request(request)
homeassistant  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_transports/default.py", line 376, in handle_async_request
homeassistant  |     with map_httpcore_exceptions():
homeassistant  |          ~~~~~~~~~~~~~~~~~~~~~~~^^
homeassistant  |   File "/usr/local/lib/python3.13/contextlib.py", line 162, in __exit__
homeassistant  |     self.gen.throw(value)
homeassistant  |     ~~~~~~~~~~~~~~^^^^^^^
homeassistant  |   File "/usr/local/lib/python3.13/site-packages/httpx/_transports/default.py", line 89, in map_httpcore_exceptions
homeassistant  |     raise mapped_exc(message) from exc
homeassistant  | httpx.ReadTimeout

I'd love to help debug this, but I think more detailed error messages would be beneficial for everyone.

@danielbrunt57
Copy link
Collaborator

danielbrunt57 commented Dec 7, 2024

Yes, it' a connection issue:

httpx.ReadTimeout (Timed out while receiving data from the host.)

Your log mentions line 1049 in config_flow. It's inside class AlexaMediaAuthorizationProxyView(HomeAssistantView), specifically check_auth(cls) """Wrap authentication into the handler."""

    @classmethod
    def check_auth(cls):
        """Wrap authentication into the handler."""

        async def wrapped(request, **kwargs):
            """Notify that the API is running."""
            hass = request.app["hass"]
            success = False
            if (
                request.remote not in cls.known_ips
                or (datetime.datetime.now() - cls.known_ips.get(request.remote)).seconds
                > cls.auth_seconds
            ):
                try:
                    flow_id = request.url.query["config_flow_id"]
                except KeyError as ex:
                    raise Unauthorized() from ex
                for flow in hass.config_entries.flow.async_progress():
                    if flow["flow_id"] == flow_id:
                        _LOGGER.debug(
                            "Found flow_id; adding %s to known_ips for %s seconds",
                            request.remote,
                            cls.auth_seconds,
                        )
                        success = True
                if not success:
                    raise Unauthorized()
                cls.known_ips[request.remote] = datetime.datetime.now()
            try:
                return await cls.handler(request, **kwargs)
            except httpx.ConnectError as ex:  # pylyint: disable=broad-except
                _LOGGER.warning("Detected Connection error: %s", ex)
                return web_response.Response(
                    headers={"content-type": "text/html"},
                    text="Connection Error! Please try refreshing. "
                    + "If this persists, please report this error to "
                    + f"<a href={ISSUE_URL}>here</a>:<br /><pre>{ex}</pre>",
                )

        return wrapped

And specifically here:

            try:
                return await cls.handler(request, **kwargs)
            except httpx.ConnectError as ex:  # pylyint: disable=broad-except
                _LOGGER.warning("Detected Connection error: %s", ex)
                return web_response.Response(
                    headers={"content-type": "text/html"},
                    text="Connection Error! Please try refreshing. "
                    + "If this persists, please report this error to "
                    + f"<a href={ISSUE_URL}>here</a>:<br /><pre>{ex}</pre>",
                )

As you can see, it catches httpx.ConnectError but not httpx.ReadTimeout.
However, code can only graciously catch the error and log it's own entry thus preventing the dump you see above but in the end, a ReadTimeout did occur and the httpx attempt did fail.

So why is proxy authentication timing out?

HTTPX is careful to enforce timeouts everywhere by default. The default behavior is to raise a TimeoutException after 5 seconds of network inactivity.
Strange errors with asynchronous requests

@danielbrunt57
Copy link
Collaborator

The should be (hopefully) a LOGGER.debug entry before the above with info about the specific request.

@danielbrunt57
Copy link
Collaborator

danielbrunt57 commented Dec 7, 2024

ChatGPT tells me this:

From the provided code, it is evident that httpx.AsyncClient is being used to perform HTTP requests. By default, the timeout in httpx for each individual connection is 5 seconds.

To increase the timeout in the given implementation, you would need to configure the httpx.AsyncClient used in the AuthCaptureProxy class. Specifically, you can modify the session_factory in the constructor to include a timeout parameter.

Here’s how you can adjust the timeout to, for example, 30 seconds:

Update the AuthCaptureProxy initialization
Modify the session_factory initialization in the constructor to include a custom timeout:

from httpx import Timeout

class AuthCaptureProxy:
    def __init__(
        self,
        proxy_url: URL,
        host_url: URL,
        session: Optional[httpx.AsyncClient] = None,
        session_factory: Optional[Callable[[], httpx.AsyncClient]] = None,
        preserve_headers: bool = False,
    ) -> None:
        self._preserve_headers = preserve_headers
        self.session_factory: Callable[[], httpx.AsyncClient] = session_factory or (
            lambda: httpx.AsyncClient(
                verify=ssl_context,
                timeout=Timeout(30.0)  # Set timeout to 30 seconds
            )
        )
        self.session: httpx.AsyncClient = session if session else self.session_factory()
        self._proxy_url: URL = proxy_url
        self._host_url: URL = host_url

Key Points:
httpx.Timeout provides fine-grained control over different types of timeouts (connect, read, write, pool). You can customize these further:

timeout=Timeout(
    connect=10.0,  # Time to establish a connection
    read=30.0,     # Time to read data
    write=30.0,    # Time to send data
    pool=5.0       # Time to wait for a free connection from the pool
)

You can still pass a custom httpx.AsyncClient via the session parameter if needed. The session_factory is used as a fallback.

By implementing this change, all requests made through this AuthCaptureProxy instance will use the updated timeout value.

@danielbrunt57
Copy link
Collaborator

danielbrunt57 commented Dec 7, 2024

Here is an untested authcaptureproxy.py modified with timeout=Timeout(30.0) as per ChatGPT: auth_capture_proxy.zip
You'll find the file in /usr/local/lib/python3.13/site-packages/authcaptureproxy/

@danielbrunt57
Copy link
Collaborator

You can also replace lines 1020-1059 in /homeassistant/custom_components/alexa_media/config_flow.py
to graciously log the timeout with:

    @classmethod
    def check_auth(cls):
        """Wrap authentication into the handler."""

        async def wrapped(request, **kwargs):
            """Notify that the API is running."""
            hass = request.app["hass"]
            success = False
            if (
                request.remote not in cls.known_ips
                or (datetime.datetime.now() - cls.known_ips.get(request.remote)).seconds
                > cls.auth_seconds
            ):
                try:
                    flow_id = request.url.query["config_flow_id"]
                except KeyError as ex:
                    raise Unauthorized() from ex
                for flow in hass.config_entries.flow.async_progress():
                    if flow["flow_id"] == flow_id:
                        _LOGGER.debug(
                            "Found flow_id; adding %s to known_ips for %s seconds",
                            request.remote,
                            cls.auth_seconds,
                        )
                        success = True
                if not success:
                    raise Unauthorized()
                cls.known_ips[request.remote] = datetime.datetime.now()
            try:
                return await cls.handler(request, **kwargs)
            except httpx.ConnectError as ex:  # pylyint: disable=broad-except
                _LOGGER.warning("HTTPX connection error in check_auth: %s", ex)
                return web_response.Response(
                    headers={"content-type": "text/html"},
                    text="Connection Error! (ConnectError) Please try refreshing. "
                    + "If this persists, please report this error to "
                    + f"<a href={ISSUE_URL}>here</a>:<br /><pre>{ex}</pre>",
                )
            except httpx.ReadTimeout as ex:  # pylyint: disable=broad-except
                _LOGGER.warning("HTTPX connection timed out in check_auth while receiving data from the host: %s", ex)
                return web_response.Response(
                    headers={"content-type": "text/html"},
                    text="Connection Error! (ReadTimeout) Please try refreshing. "
                    + "If this persists, please report this error to "
                    + f"<a href={ISSUE_URL}>here</a>:<br /><pre>{ex}</pre>",
                )

        return wrapped

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

No branches or pull requests