/
server.py
707 lines (564 loc) · 23.7 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# Copyright 2018-2020 Streamlit Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
import threading
import socket
import sys
import errno
import traceback
import click
from enum import Enum
from typing import Any, Dict, Optional, TYPE_CHECKING
import tornado.concurrent
import tornado.gen
import tornado.ioloop
import tornado.netutil
import tornado.web
import tornado.websocket
from streamlit import config
from streamlit import file_util
from streamlit.config_option import ConfigOption
from streamlit.forward_msg_cache import ForwardMsgCache
from streamlit.forward_msg_cache import create_reference_msg
from streamlit.forward_msg_cache import populate_hash_if_needed
from streamlit.media_file_manager import media_file_manager
from streamlit.report_session import ReportSession
from streamlit.uploaded_file_manager import UploadedFileManager
from streamlit.logger import get_logger
from streamlit.components.v1.components import ComponentRegistry
from streamlit.components.v1.components import ComponentRequestHandler
from streamlit.proto.BackMsg_pb2 import BackMsg
from streamlit.proto.ForwardMsg_pb2 import ForwardMsg
from streamlit.server.upload_file_request_handler import UploadFileRequestHandler
from streamlit.server.routes import AddSlashHandler
from streamlit.server.routes import AssetsFileHandler
from streamlit.server.routes import DebugHandler
from streamlit.server.routes import HealthHandler
from streamlit.server.routes import MediaFileHandler
from streamlit.server.routes import MessageCacheHandler
from streamlit.server.routes import MetricsHandler
from streamlit.server.routes import StaticFileHandler
from streamlit.server.server_util import MESSAGE_SIZE_LIMIT
from streamlit.server.server_util import is_cacheable_msg
from streamlit.server.server_util import is_url_from_allowed_origins
from streamlit.server.server_util import make_url_path_regex
from streamlit.server.server_util import serialize_forward_msg
if TYPE_CHECKING:
from streamlit.report import Report
LOGGER = get_logger(__name__)
TORNADO_SETTINGS = {
"compress_response": True, # Gzip HTTP responses.
"websocket_ping_interval": 20, # Ping every 20s to keep WS alive.
"websocket_ping_timeout": 30, # Pings should be responded to within 30s.
"websocket_max_message_size": MESSAGE_SIZE_LIMIT, # Up the WS size limit.
}
# When server.port is not available it will look for the next available port
# up to MAX_PORT_SEARCH_RETRIES.
MAX_PORT_SEARCH_RETRIES = 100
# When server.address starts with this prefix, the server will bind
# to an unix socket.
UNIX_SOCKET_PREFIX = "unix://"
class SessionInfo(object):
"""Type stored in our _session_info_by_id dict.
For each ReportSession, the server tracks that session's
report_run_count. This is used to track the age of messages in
the ForwardMsgCache.
"""
def __init__(self, ws, session):
"""Initialize a SessionInfo instance.
Parameters
----------
session : ReportSession
The ReportSession object.
ws : _BrowserWebSocketHandler
The websocket that owns this report.
"""
self.session = session
self.ws = ws
self.report_run_count = 0
class State(Enum):
INITIAL = "INITIAL"
WAITING_FOR_FIRST_BROWSER = "WAITING_FOR_FIRST_BROWSER"
ONE_OR_MORE_BROWSERS_CONNECTED = "ONE_OR_MORE_BROWSERS_CONNECTED"
NO_BROWSERS_CONNECTED = "NO_BROWSERS_CONNECTED"
STOPPING = "STOPPING"
STOPPED = "STOPPED"
class RetriesExceeded(Exception):
pass
def server_port_is_manually_set():
return config.is_manually_set("server.port")
def server_address_is_unix_socket():
address = config.get_option("server.address")
return address and address.startswith(UNIX_SOCKET_PREFIX)
def start_listening(app):
"""Makes the server start listening at the configured port.
In case the port is already taken it tries listening to the next available
port. It will error after MAX_PORT_SEARCH_RETRIES attempts.
"""
http_server = tornado.httpserver.HTTPServer(
app, max_buffer_size=config.get_option("server.maxUploadSize") * 1024 * 1024
)
if server_address_is_unix_socket():
start_listening_unix_socket(http_server)
else:
start_listening_tcp_socket(http_server)
def start_listening_unix_socket(http_server):
address = config.get_option("server.address")
file_name = os.path.expanduser(address[len(UNIX_SOCKET_PREFIX) :])
unix_socket = tornado.netutil.bind_unix_socket(file_name)
http_server.add_socket(unix_socket)
def start_listening_tcp_socket(http_server):
call_count = 0
while call_count < MAX_PORT_SEARCH_RETRIES:
address = config.get_option("server.address")
port = config.get_option("server.port")
try:
http_server.listen(port, address)
break # It worked! So let's break out of the loop.
except (OSError, socket.error) as e:
if e.errno == errno.EADDRINUSE:
if server_port_is_manually_set():
LOGGER.error("Port %s is already in use", port)
sys.exit(1)
else:
LOGGER.debug(
"Port %s already in use, trying to use the next one.", port
)
port += 1
# Save port 3000 because it is used for the development
# server in the front end.
if port == 3000:
port += 1
config._set_option(
"server.port", port, ConfigOption.STREAMLIT_DEFINITION
)
call_count += 1
else:
raise
if call_count >= MAX_PORT_SEARCH_RETRIES:
raise RetriesExceeded(
"Cannot start Streamlit server. Port %s is already in use, and "
"Streamlit was unable to find a free port after %s attempts.",
port,
MAX_PORT_SEARCH_RETRIES,
)
class Server(object):
_singleton = None # type: Optional[Server]
@classmethod
def get_current(cls):
"""
Returns
-------
Server
The singleton Server object.
"""
if cls._singleton is None:
raise RuntimeError("Server has not been initialized yet")
return Server._singleton
def __init__(self, ioloop, script_path, command_line):
"""Create the server. It won't be started yet.
Parameters
----------
ioloop : tornado.ioloop.IOLoop
script_path : str
command_line : str
"""
if Server._singleton is not None:
raise RuntimeError("Server already initialized. Use .get_current() instead")
Server._singleton = self
_set_tornado_log_levels()
self._ioloop = ioloop
self._script_path = script_path
self._command_line = command_line
media_file_manager.set_ioloop(ioloop=self._ioloop)
# Mapping of ReportSession.id -> SessionInfo.
self._session_info_by_id = {}
self._must_stop = threading.Event()
self._state = None
self._set_state(State.INITIAL)
self._message_cache = ForwardMsgCache()
self._uploaded_file_mgr = UploadedFileManager()
self._uploaded_file_mgr.on_files_added.connect(self._on_file_uploaded)
self._report = None # type: Optional[Report]
self._preheated_session_id = None # type: Optional[str]
def _on_file_uploaded(self, file):
"""Event handler for UploadedFileManager.on_file_added.
When a file is uploaded by a user, schedule a re-run of the
corresponding ReportSession.
Parameters
----------
file : File
The file that was just uploaded.
"""
session_info = self._get_session_info(file.session_id)
if session_info is not None:
session_info.session.request_rerun()
else:
# If an uploaded file doesn't belong to an existing session,
# remove it so it doesn't stick around forever.
self._uploaded_file_mgr.remove_files(file.session_id, file.widget_id)
def _get_session_info(self, session_id):
"""Return the SessionInfo with the given id, or None if no such
session exists.
Parameters
----------
session_id : str
Returns
-------
SessionInfo or None
"""
return self._session_info_by_id.get(session_id, None)
def start(self, on_started):
"""Start the server.
Parameters
----------
on_started : callable
A callback that will be called when the server's run-loop
has started, and the server is ready to begin receiving clients.
"""
if self._state != State.INITIAL:
raise RuntimeError("Server has already been started")
LOGGER.debug("Starting server...")
app = self._create_app()
start_listening(app)
port = config.get_option("server.port")
LOGGER.debug("Server started on port %s", port)
self._ioloop.spawn_callback(self._loop_coroutine, on_started)
def get_debug(self) -> Dict[str, Dict[str, Any]]:
if self._report:
return {"report": self._report.get_debug()}
return {}
def _create_app(self):
"""Create our tornado web app.
Returns
-------
tornado.web.Application
"""
base = config.get_option("server.baseUrlPath")
routes = [
(
make_url_path_regex(base, "stream"),
_BrowserWebSocketHandler,
dict(server=self),
),
(
make_url_path_regex(base, "healthz"),
HealthHandler,
dict(callback=lambda: self.is_ready_for_browser_connection),
),
(make_url_path_regex(base, "debugz"), DebugHandler, dict(server=self)),
(make_url_path_regex(base, "metrics"), MetricsHandler),
(
make_url_path_regex(base, "message"),
MessageCacheHandler,
dict(cache=self._message_cache),
),
(
make_url_path_regex(base, "upload_file"),
UploadFileRequestHandler,
dict(file_mgr=self._uploaded_file_mgr),
),
(
make_url_path_regex(base, "assets/(.*)"),
AssetsFileHandler,
{"path": "%s/" % file_util.get_assets_dir()},
),
(make_url_path_regex(base, "media/(.*)"), MediaFileHandler),
(
make_url_path_regex(base, "component/(.*)"),
ComponentRequestHandler,
dict(registry=ComponentRegistry.instance()),
),
]
if config.get_option("global.developmentMode"):
LOGGER.debug("Serving static content from the Node dev server")
else:
static_path = file_util.get_static_dir()
LOGGER.debug("Serving static content from %s", static_path)
routes.extend(
[
(
make_url_path_regex(base, "(.*)"),
StaticFileHandler,
{"path": "%s/" % static_path, "default_filename": "index.html"},
),
(make_url_path_regex(base, trailing_slash=False), AddSlashHandler),
]
)
return tornado.web.Application(
routes, # type: ignore[arg-type]
cookie_secret=config.get_option("server.cookieSecret"),
xsrf_cookies=config.get_option("server.enableXsrfProtection"),
**TORNADO_SETTINGS, # type: ignore[arg-type]
)
def _set_state(self, new_state):
LOGGER.debug("Server state: %s -> %s" % (self._state, new_state))
self._state = new_state
@property
def is_ready_for_browser_connection(self):
return self._state not in (State.INITIAL, State.STOPPING, State.STOPPED)
@property
def browser_is_connected(self):
return self._state == State.ONE_OR_MORE_BROWSERS_CONNECTED
@property
def is_running_hello(self):
from streamlit.hello import hello
return self._script_path == hello.__file__
@tornado.gen.coroutine
def _loop_coroutine(self, on_started=None):
try:
if self._state == State.INITIAL:
self._set_state(State.WAITING_FOR_FIRST_BROWSER)
elif self._state == State.ONE_OR_MORE_BROWSERS_CONNECTED:
pass
else:
raise RuntimeError("Bad server state at start: %s" % self._state)
if on_started is not None:
on_started(self)
while not self._must_stop.is_set():
if self._state == State.WAITING_FOR_FIRST_BROWSER:
pass
elif self._state == State.ONE_OR_MORE_BROWSERS_CONNECTED:
# Shallow-clone our sessions into a list, so we can iterate
# over it and not worry about whether it's being changed
# outside this coroutine.
session_infos = list(self._session_info_by_id.values())
for session_info in session_infos:
if session_info.ws is None:
# Preheated.
continue
msg_list = session_info.session.flush_browser_queue()
for msg in msg_list:
try:
self._send_message(session_info, msg)
except tornado.websocket.WebSocketClosedError:
self._close_report_session(session_info.session.id)
yield
yield
elif self._state == State.NO_BROWSERS_CONNECTED:
pass
else:
# Break out of the thread loop if we encounter any other state.
break
yield tornado.gen.sleep(0.01)
# Shut down all ReportSessions
for session_info in list(self._session_info_by_id.values()):
session_info.session.shutdown()
self._set_state(State.STOPPED)
except Exception as e:
# Can't just re-raise here because co-routines use Tornado
# exceptions for control flow, which appears to swallow the reraised
# exception.
traceback.print_exc()
LOGGER.info(
"""
Please report this bug at https://github.com/streamlit/streamlit/issues.
"""
)
finally:
self._on_stopped()
def _send_message(self, session_info, msg):
"""Send a message to a client.
If the client is likely to have already cached the message, we may
instead send a "reference" message that contains only the hash of the
message.
Parameters
----------
session_info : SessionInfo
The SessionInfo associated with websocket
msg : ForwardMsg
The message to send to the client
"""
msg.metadata.cacheable = is_cacheable_msg(msg)
msg_to_send = msg
if msg.metadata.cacheable:
populate_hash_if_needed(msg)
if self._message_cache.has_message_reference(
msg, session_info.session, session_info.report_run_count
):
# This session has probably cached this message. Send
# a reference instead.
LOGGER.debug("Sending cached message ref (hash=%s)" % msg.hash)
msg_to_send = create_reference_msg(msg)
# Cache the message so it can be referenced in the future.
# If the message is already cached, this will reset its
# age.
LOGGER.debug("Caching message (hash=%s)" % msg.hash)
self._message_cache.add_message(
msg, session_info.session, session_info.report_run_count
)
# If this was a `report_finished` message, we increment the
# report_run_count for this session, and update the cache
if (
msg.WhichOneof("type") == "report_finished"
and msg.report_finished == ForwardMsg.FINISHED_SUCCESSFULLY
):
LOGGER.debug(
"Report finished successfully; "
"removing expired entries from MessageCache "
"(max_age=%s)",
config.get_option("global.maxCachedMessageAge"),
)
session_info.report_run_count += 1
self._message_cache.remove_expired_session_entries(
session_info.session, session_info.report_run_count
)
# Ship it off!
session_info.ws.write_message(serialize_forward_msg(msg_to_send), binary=True)
def stop(self):
click.secho(" Stopping...", fg="blue")
self._set_state(State.STOPPING)
self._must_stop.set()
def _on_stopped(self):
"""Called when our runloop is exiting, to shut down the ioloop.
This will end our process.
(Tests can patch this method out, to prevent the test's ioloop
from being shutdown.)
"""
self._ioloop.stop()
def add_preheated_report_session(self):
"""Register a fake browser with the server and run the script.
This is used to start running the user's script even before the first
browser connects.
"""
session = self._create_or_reuse_report_session(ws=None)
session.handle_rerun_script_request(is_preheat=True)
def _create_or_reuse_report_session(self, ws):
"""Register a connected browser with the server.
Parameters
----------
ws : _BrowserWebSocketHandler or None
The newly-connected websocket handler or None if preheated
connection.
Returns
-------
ReportSession
The newly-created ReportSession for this browser connection.
"""
if self._preheated_session_id is not None:
assert len(self._session_info_by_id) == 1
assert ws is not None
session_id = self._preheated_session_id
self._preheated_session_id = None
session_info = self._session_info_by_id[session_id]
session_info.ws = ws
session = session_info.session
LOGGER.debug(
"Reused preheated session for ws %s. Session ID: %s", id(ws), session_id
)
else:
session = ReportSession(
ioloop=self._ioloop,
script_path=self._script_path,
command_line=self._command_line,
uploaded_file_manager=self._uploaded_file_mgr,
)
LOGGER.debug(
"Created new session for ws %s. Session ID: %s", id(ws), session.id
)
assert session.id not in self._session_info_by_id, (
"session.id '%s' registered multiple times!" % session.id
)
self._session_info_by_id[session.id] = SessionInfo(ws, session)
if ws is None:
self._preheated_session_id = session.id
else:
self._set_state(State.ONE_OR_MORE_BROWSERS_CONNECTED)
return session
def _close_report_session(self, session_id):
"""Shutdown and remove a ReportSession.
This function may be called multiple times for the same session,
which is not an error. (Subsequent calls just no-op.)
Parameters
----------
session_id : str
The ReportSession's id string.
"""
if session_id in self._session_info_by_id:
session_info = self._session_info_by_id[session_id]
del self._session_info_by_id[session_id]
session_info.session.shutdown()
if len(self._session_info_by_id) == 0:
self._set_state(State.NO_BROWSERS_CONNECTED)
class _BrowserWebSocketHandler(tornado.websocket.WebSocketHandler):
"""Handles a WebSocket connection from the browser"""
def initialize(self, server):
self._server = server
self._session = None
# The XSRF cookie is normally set when xsrf_form_html is used, but in a pure-Javascript application
# that does not use any regular forms we just need to read the self.xsrf_token manually to set the
# cookie as a side effect.
# See https://www.tornadoweb.org/en/stable/guide/security.html#cross-site-request-forgery-protection
# for more details.
if config.get_option("server.enableXsrfProtection"):
self.xsrf_token
def check_origin(self, origin):
"""Set up CORS."""
return super().check_origin(origin) or is_url_from_allowed_origins(origin)
def open(self):
self._session = self._server._create_or_reuse_report_session(self)
def on_close(self):
if not self._session:
return
self._server._close_report_session(self._session.id)
self._session = None
def get_compression_options(self):
"""Enable WebSocket compression.
Returning an empty dict enables websocket compression. Returning
None disables it.
(See the docstring in the parent class.)
"""
if config.get_option("server.enableWebsocketCompression"):
return {}
return None
@tornado.gen.coroutine
def on_message(self, payload):
if not self._session:
return
msg = BackMsg()
try:
msg.ParseFromString(payload)
msg_type = msg.WhichOneof("type")
LOGGER.debug("Received the following back message:\n%s", msg)
if msg_type == "cloud_upload":
yield self._session.handle_save_request(self)
elif msg_type == "rerun_script":
self._session.handle_rerun_script_request(msg.rerun_script)
elif msg_type == "clear_cache":
self._session.handle_clear_cache_request()
elif msg_type == "set_run_on_save":
self._session.handle_set_run_on_save_request(msg.set_run_on_save)
elif msg_type == "stop_report":
self._session.handle_stop_script_request()
elif msg_type == "close_connection":
if config.get_option("global.developmentMode"):
Server.get_current().stop()
else:
LOGGER.warning(
"Client tried to close connection when "
"not in development mode"
)
else:
LOGGER.warning('No handler for "%s"', msg_type)
except BaseException as e:
LOGGER.error(e)
self._session.enqueue_exception(e)
def _set_tornado_log_levels():
if not config.get_option("global.developmentMode"):
# Hide logs unless they're super important.
# Example of stuff we don't care about: 404 about .js.map files.
logging.getLogger("tornado.access").setLevel(logging.ERROR)
logging.getLogger("tornado.application").setLevel(logging.ERROR)
logging.getLogger("tornado.general").setLevel(logging.ERROR)