Applies everything staged before this call: include paths and flags, then libraries, then evaluations, in the order they were staged. The listener starts only after those succeed. The attempt consumes the staged items whether or not it succeeds. If any staged library or evaluation fails, or the server cannot listen, the session is torn down and the failure is returned.
Only one session is permitted per process. Staging calls made on other threads while the start is in progress are refused, not queued.
214{
215 enum class Refusal : uint8_t {
216 None,
217 Running,
218 Starting
219 };
220
221 Refusal refusal = Refusal::None;
222 std::optional<Lila::Address> running;
223 std::array<std::vector<std::string>, k_stage_count> staged;
224
225 g_state.update([&](State& state) {
226 if (state.instance) {
227 refusal = Refusal::Running;
228 running = state.address;
229 return false;
230 }
231 if (state.starting) {
232 refusal = Refusal::Starting;
233 return false;
234 }
235 refusal = Refusal::None;
236 staged = std::exchange(state.staged, {});
237 state.starting = true;
238 return true;
239 });
240
241 if (refusal == Refusal::Running) {
242 return refuse(
"session already running on " + where_text(running));
243 }
244 if (refusal == Refusal::Starting) {
245 return refuse(
"session is starting");
246 }
247
248 auto instance = std::make_shared<Lila::Lila>();
249 instance->add_eval_observer(&dispatch_eval, nullptr);
250
252 instance->add_include_path(path);
253 }
255 instance->add_compile_flag(flag);
256 }
257
259 instance->stop_listening();
260 g_state.update([](State& state) {
261 state.starting = false;
262 state.libraries.clear();
263 });
264 return std::unexpected(std::move(failure));
265 };
266
267 if (const auto ready = instance->initialize(true); !ready) {
268 return abandon(
Failure { .message =
"initialization failed: " + std::string(ready.error()), .tried = {} });
269 }
270
272 if (auto loaded = run_load(*instance, library); !loaded) {
273 return abandon(std::move(loaded.error()));
274 }
275 }
277 if (auto evaluated = run_eval(*instance, code); !evaluated) {
278 return abandon(std::move(evaluated.error()));
279 }
280 }
281
282 if (const auto listening = instance->listen(where); !listening) {
283 return abandon(
Failure { .message =
"listen failed: " + std::string(listening.error()), .tried = {} });
284 }
285
286 const auto* resolved = instance->address();
287 const std::optional<Lila::Address> bound = resolved ? std::optional<Lila::Address>(*resolved) : std::nullopt;
288 g_state.update([&](State& state) {
289 state.instance = instance;
290 state.address = bound;
291 state.starting = false;
292 });
293
294 LILA_INFO(Lila::Emitter::SYSTEM,
"start_session: running on " + where_text(bound));
295 return {};
296}
std::expected< T, Failure > Result
Result of a Host call.
void refuse(std::string_view message)
Why a Host call was refused, with every path that was checked.