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

[Shortfin][SD] Error out on invalid ports #1029

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions shortfin/python/shortfin_apps/sd/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ def get_modules(args, model_config, flagfile, td_spec):
return vmfbs, params


def is_port_valid(port):
max_port = 65535
if port < 1 or port > max_port:
print(
f"Error: Invalid port specified ({port}), expected a value between 1 and {max_port}"
)
return False
return True


def main(argv, log_config=UVICORN_LOG_CONFIG):
parser = argparse.ArgumentParser()
parser.add_argument("--host", type=str, default=None)
Expand Down Expand Up @@ -403,6 +413,9 @@ def main(argv, log_config=UVICORN_LOG_CONFIG):
help="Use tunings for attention and matmul ops. 0 to disable.",
)
args = parser.parse_args(argv)
if not is_port_valid(args.port):
exit(3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting flashbacks of "error 37" from a couple years ago but this is good enough for now.


if not args.artifacts_dir:
home = Path.home()
artdir = home / ".cache" / "shark"
Expand Down
19 changes: 16 additions & 3 deletions shortfin/python/shortfin_apps/sd/simple_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,15 @@ async def async_range(count):
await asyncio.sleep(0.0)


def check_health(url):
def is_connected(host, port):
max_port = 65535
if port < 1 or port > max_port:
print(
f"Error: Invalid port specified ({port}), expected a value between 1 and {max_port}"
)
return False

url = f"{host}:{port}"
ready = False
print("Waiting for server.", end=None)
while not ready:
Expand All @@ -192,6 +200,8 @@ def check_health(url):
time.sleep(2)
print(".", end=None)

return True


def main():
p = argparse.ArgumentParser()
Expand Down Expand Up @@ -222,7 +232,7 @@ def main():
p.add_argument(
"--host", type=str, default="http://0.0.0.0", help="Server host address."
)
p.add_argument("--port", type=str, default="8000", help="Server port")
p.add_argument("--port", type=int, default=8000, help="Server port")
p.add_argument(
"--steps",
type=int,
Expand All @@ -235,7 +245,10 @@ def main():
help="Start as an example CLI client instead of sending static requests.",
)
args = p.parse_args()
check_health(f"{args.host}:{args.port}")

if not is_connected(args.host, args.port):
exit(3)

if args.interactive:
asyncio.run(interactive(args))
else:
Expand Down
Loading