From 580f6657b968b564df6e59b07924624720252534 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Wed, 4 Feb 2026 15:38:51 -0800 Subject: [PATCH] fix segfault --- build.zig.zon | 4 ++-- src/alexa.zig | 3 +++ src/homeassistant.zig | 10 +++++++--- src/main.zig | 39 +++++++++++++++++++++------------------ 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 407acf2..9105358 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -9,8 +9,8 @@ .hash = "controlr-0.1.0-upFm0LtgAAAo85RiRDa0WmSrORIhAa5bCF8UdT9rDyUk", }, .lambda_zig = .{ - .url = "git+https://git.lerch.org/lobo/lambda-zig#f444697d93244425fa799023d0e7adf80ecaa1de", - .hash = "lambda_zig-0.1.0-_G43_9VdAQBdYVii9jenUiYxPssqZudPv23LEJVA2W0b", + .url = "git+https://git.lerch.org/lobo/lambda-zig#5c36a2c979b0bd4f0cbda3d64fb997f341085bc9", + .hash = "lambda_zig-0.1.0-_G43_2ReAQDdEirp5hBDtuKAtAPzWjleCp9nw524BCdZ", }, .aws = .{ .url = "git+https://git.lerch.org/lobo/aws-sdk-for-zig#5c7aed071f6251d53a1627080a21d604ff58f0a5", diff --git a/src/alexa.zig b/src/alexa.zig index b69a5f2..8e68960 100644 --- a/src/alexa.zig +++ b/src/alexa.zig @@ -64,6 +64,9 @@ pub fn parseContext(json_value: json.Value) ?Context { /// GET {apiEndpoint}/v2/devices/{deviceId}/settings/System.timeZone /// Returns timezone name like "America/Los_Angeles" or null on failure. /// Caller owns the returned memory. +/// +/// NOTE: This function is currently not used because the API call takes ~1 second +/// which is too slow for our 3 second Lambda timeout. See resolveTimezone() in main.zig. pub fn fetchTimezone( allocator: Allocator, http_interface: homeassistant.HttpClientInterface, diff --git a/src/homeassistant.zig b/src/homeassistant.zig index e837428..66aab40 100644 --- a/src/homeassistant.zig +++ b/src/homeassistant.zig @@ -251,7 +251,7 @@ pub const Client = struct { base_url: []const u8, token: []const u8, http_interface: HttpClientInterface, - client: ?HttpClient = null, + client: ?*HttpClient = null, /// Normal initialization. If you are writing tests and need a mock /// implemenation for unit tests, use MockHttpClient with the initInterface @@ -261,7 +261,8 @@ pub const Client = struct { base_url: []const u8, token: []const u8, ) Client { - var client = HttpClient.init(allocator); + const client = allocator.create(HttpClient) catch @panic("OOM"); + client.* = HttpClient.init(allocator); return .{ .allocator = allocator, .base_url = base_url, @@ -288,7 +289,10 @@ pub const Client = struct { } pub fn deinit(self: Client) void { - if (self.client) |c| c.deinit(); + if (self.client) |c| { + c.deinit(); + self.allocator.destroy(c); + } } /// Fetch all entity states from Home Assistant pub fn getStates(self: *Client) !struct { entities: []Entity, parsed: json.Parsed(json.Value) } { diff --git a/src/main.zig b/src/main.zig index 7c3bf0a..2033d90 100644 --- a/src/main.zig +++ b/src/main.zig @@ -55,7 +55,6 @@ pub fn main() !u8 { /// Main Alexa request handler fn handler(allocator: std.mem.Allocator, event_data: []const u8, config: Config) anyerror![]const u8 { - log.info("Received Alexa request: {d} bytes", .{event_data.len}); // Parse the Alexa request const parsed = json.parseFromSlice(json.Value, allocator, event_data, .{}) catch |err| { @@ -354,27 +353,31 @@ fn extractSlotNumber(slots: ?json.Value, slot_name: []const u8) ?f32 { } /// Resolve the UTC offset for timezone conversion. -/// In Lambda mode: tries Alexa Settings API, then falls back to local timezone. +/// In Lambda mode: uses TZ env var (Alexa API lookup disabled - too slow ~1s). /// In local mode: uses TZ env var or /etc/timezone. fn resolveTimezone(allocator: std.mem.Allocator, parsed_value: json.Value, config: Config) ?i32 { - // First try to get timezone from Alexa context (Lambda mode) - if (alexa.parseContext(parsed_value)) |context| { - // Need HTTP client for Alexa API - const ha_url = config.home_assistant_url orelse return resolveLocalTimezone(allocator); - const ha_token = config.home_assistant_token orelse return resolveLocalTimezone(allocator); + _ = parsed_value; + _ = config; - var ha_client = homeassistant.Client.init(allocator, ha_url, ha_token); - defer ha_client.deinit(); + // NOTE: Alexa Settings API timezone lookup is disabled because it takes ~1 second + // which is too much of our 3 second Lambda timeout budget. Instead, we use the + // TZ environment variable set in Lambda configuration. + // + // To re-enable Alexa timezone lookup, uncomment the following block: + // + // if (alexa.parseContext(parsed_value)) |context| { + // const ha_url = config.home_assistant_url orelse return resolveLocalTimezone(allocator); + // const ha_token = config.home_assistant_token orelse return resolveLocalTimezone(allocator); + // var ha_client = homeassistant.Client.init(allocator, ha_url, ha_token); + // defer ha_client.deinit(); + // if (alexa.fetchTimezone(allocator, ha_client.httpInterface(), context)) |tz_name| { + // defer allocator.free(tz_name); + // if (timezone.getUtcOffset(allocator, tz_name)) |offset| { + // return offset; + // } + // } + // } - if (alexa.fetchTimezone(allocator, ha_client.http_interface, context)) |tz_name| { - defer allocator.free(tz_name); - if (timezone.getUtcOffset(allocator, tz_name)) |offset| { - return offset; - } - } - } - - // Fall back to local timezone return resolveLocalTimezone(allocator); }