-
Notifications
You must be signed in to change notification settings - Fork 7
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
Use dynamic ports for BackgroundHTTPServer #223
Conversation
For the record (in case you get the idea during review), this: -srv = util.BackgroundHTTPServer(virt.NETWORK_HOST, 0)
-with srv:
+with util.BackgroundHTTPServer(virt.NETWORK_HOST, 0) as srv: doesn't work - the I guess we could |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we could return self from enter, but maybe that would be even more confusing for readers / maintainers, as there would be 2 not-so-obvious ways to get the object.
What do you mean by there would be 2 not-so-obvious ways to get the object
? If we return self
from __enter__
and use it only in with
+as
, then there would be basically "only" one way we would be okay with.
lib/virt.py
Outdated
srv = util.BackgroundHTTPServer(NETWORK_HOST, 0) | ||
srv.add_dir(repo, 'repo') | ||
stack.enter_context(srv) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent with other BackgroundHTTPServer
use cases, shouldn't it be like this?
srv = util.BackgroundHTTPServer(NETWORK_HOST, 0) | |
srv.add_dir(repo, 'repo') | |
stack.enter_context(srv) | |
srv = util.BackgroundHTTPServer(NETWORK_HOST, 0) | |
stack.enter_context(srv) | |
srv.add_dir(repo, 'repo') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's better to add the file before starting the server if possible (to fully eliminate any chance of race conditions).
In the other cases, it's not possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not? For example in hardening/anaconda
you can add remediation-ds.xml
outside of srv
context and so add_file
before context as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's a good point - the anaconda use case could probably be done before starting the server.
I'll fix it and add .start()
to the server, so that object initialization is more clearly decoupled from starting the server and we don't rely on the sneaky context manager behavior.
The problem is the
with some other http-server-unrelated code in between. It's a subtle difference but we would essentially be doing the equivalent of with ExitStack() as stack:
f = open('file.txt')
stack.enter_context(f) which is not the same as with ExitStack() as stack:
stack.enter_context(open('file.txt')) due to how the context manager is called. Maybe I should side-step this issue and a with util.BackgroundHTTPServer('127.0.0.1', 0) as srv:
srv.add_file(...)
srv.start() # can be swapped with add_file()
... or srv = util.BackgroundHTTPServer('127.0.0.1', 0)
srv.add_file(...)
stack.enter_context(srv.start()) # can be swapped with add_file() ? |
I've pushed an alternate approach - have a look at the module help for Hopefully this makes things a bit clearer. |
Tested on |
Yep, code looks good Nit-pick: f8af92c is unnecessary now. Could you drop it? |
Signed-off-by: Jiri Jaburek <comps@nomail.dom>
This should hopefully be less confusing in terms of usage with a Context Manager. Signed-off-by: Jiri Jaburek <comps@nomail.dom>
Done. |
This is a follow-up to 3277a78 from #210.
I've tested
(the main/only tests impacted by this) and they all seem to pass/warn just fine, so I presume it works.
I also ran some multi-profile runs of
anaconda
/image-builder
to make sure there isn't anything persistent that would break 2 or more tests running sequentially on a single system.