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

Calling BaseRequest.host can do blocking I/O if the host header is missing #9308

Open
bdraco opened this issue Sep 27, 2024 · 4 comments
Open
Labels

Comments

@bdraco
Copy link
Member

bdraco commented Sep 27, 2024

return socket.getfqdn()

There is a fallback to socket.getfqdn() which is not async safe and will block the loop while its resolved

@bdraco bdraco added the bug label Sep 27, 2024
@bdraco
Copy link
Member Author

bdraco commented Sep 27, 2024

Fix for Home Assistant is home-assistant/core#126882

@bdraco
Copy link
Member Author

bdraco commented Sep 27, 2024

I'm not sure how likely it would be that its not cached locally.. but if its missing from /etc/hosts it could have to resolve it

@bdraco
Copy link
Member Author

bdraco commented Sep 27, 2024

Actually it appears quite likely since the first system I tested it on did blocking I/O

strace python -c "import socket; socket.getfqdn()'

open("/etc/hosts", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
read(3, "127.0.0.1\tlocalhost\n::1\tlocalhos"..., 1024) = 194
read(3, "", 1024)                       = 0
close(3)                                = 0
open("/etc/resolv.conf", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
read(3, "# Generated by Docker Engine.\n# "..., 248) = 248
read(3, " [nameservers search options]\n", 248) = 30
read(3, "", 248)                        = 0
close(3)                                = 0
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 3
bind(3, {sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
sendto(3, "\10\23\1\0\0\1\0\0\0\0\0\0\rhomeassistant\5local"..., 45, MSG_NOSIGNAL, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("172.30.32.3")}, 16) = 45
sendto(3, "\t\305\1\0\0\1\0\0\0\0\0\0\rhomeassistant\5local"..., 45, MSG_NOSIGNAL, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("172.30.32.3")}, 16) = 45
poll([{fd=-1}, {fd=-1}, {fd=3, events=POLLIN}], 3, 5000) = 1 ([{fd=3, revents=POLLIN}])
recvmsg(3, {msg_name={sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("172.30.32.3")}, msg_namelen=16, msg_iov=[{iov_base="\10\23\205\0\0\1\0\1\0\0\0\0\rhomeassistant\5local"..., iov_len=4800}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 88
recvmsg(3, {msg_name={sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("172.30.32.3")}, msg_namelen=16, msg_iov=[{iov_base="\t\305\205\0\0\1\0\0\0\0\0\0\rhomeassistant\5local"..., iov_len=4800}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 45
close(3)                                = 0
open("/etc/hosts", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
read(3, "127.0.0.1\tlocalhost\n::1\tlocalhos"..., 1024) = 194
read(3, "", 1024)                       = 0
close(3)                                = 0
open("/etc/resolv.conf", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
read(3, "# Generated by Docker Engine.\n# "..., 248) = 248
read(3, " [nameservers search options]\n", 248) = 30
read(3, "", 248)                        = 0
close(3)                                = 0
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 3
bind(3, {sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
sendto(3, "O\337\1\0\0\1\0\0\0\0\0\0\0011\00232\00230\003172\7in-addr"..., 42, MSG_NOSIGNAL, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("172.30.32.3")}, 16) = 42
poll([{fd=-1}, {fd=3, events=POLLIN}], 2, 5000) = 1 ([{fd=3, revents=POLLIN}])
recvmsg(3, {msg_name={sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("172.30.32.3")}, msg_namelen=16, msg_iov=[{iov_base="O\337\205\0\0\1\0\n\0\0\0\0\0011\00232\00230\003172\7in-addr"..., iov_len=512}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 330
close(3)                                = 0
rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK, sa_restorer=0x7f324d536ea8}, {sa_handler=0x7f324cfe3de8, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK, sa_restorer=0x7f324d536ea8}, 8) = 0

bdraco added a commit to home-assistant/core that referenced this issue Sep 27, 2024
request.host can fallback to doing blocking I/O because of
aio-libs/aiohttp#9308
@bdraco
Copy link
Member Author

bdraco commented Oct 1, 2024

I haven't come up with a solution to this that isn't a breaking change to remove the blocking method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

1 participant