Skip to content

Commit 5be2aa3

Browse files
committed
make sure to close sockets that we open to detect cloud environment (elastic#1134)
fixes elastic#1133
1 parent b30532f commit 5be2aa3

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

‎elasticapm/utils/cloud.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def aws_metadata():
4545
try:
4646
# This will throw an error if the metadata server isn't available,
4747
# and will be quiet in the logs, unlike urllib3
48-
socket.create_connection(("169.254.169.254", 80), 0.1)
48+
with socket.create_connection(("169.254.169.254", 80), 0.1):
49+
pass
4950

5051
try:
5152
# This whole block is almost unnecessary. IMDSv1 will be supported
@@ -135,7 +136,8 @@ def azure_metadata():
135136
try:
136137
# This will throw an error if the metadata server isn't available,
137138
# and will be quiet in the logs, unlike urllib3
138-
socket.create_connection(("169.254.169.254", 80), 0.1)
139+
with socket.create_connection(("169.254.169.254", 80), 0.1):
140+
pass
139141

140142
# Can't use newest metadata service version, as it's not guaranteed
141143
# to be available in all regions

0 commit comments

Comments
 (0)