Skip to content

Commit

Permalink
Return thread handles for gossip service
Browse files Browse the repository at this point in the history
Allocating a pointer to hold the thread handles & limited shared state
in order to properly deallocate the associated resources and join
the threads correctly is more appropriate than spawning a whole thread
for that purpose.
  • Loading branch information
InKryption committed May 30, 2024
1 parent f321947 commit fcf40c7
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 83 deletions.
17 changes: 11 additions & 6 deletions src/cmd/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ fn gossip() !void {
&.{},
);
defer gossip_service.deinit();
try runGossipWithConfigValues(&gossip_service);

const gossip_run_state = try runGossipWithConfigValues(&gossip_service);
defer gossip_run_state.deinit(&gossip_service);
}

/// entrypoint to run a full solana validator
Expand Down Expand Up @@ -406,7 +408,9 @@ fn validator() !void {
&.{.{ .tag = socket_tag.REPAIR, .port = repair_port }},
);
defer gossip_service.deinit();
const gossip_handle = try std.Thread.spawn(.{}, runGossipWithConfigValues, .{&gossip_service});

const gossip_run_state = try runGossipWithConfigValues(&gossip_service);
defer gossip_run_state.deinit(&gossip_service);

// repair
var repair_socket = try Socket.create(network.AddressFamily.ipv4, network.Protocol.udp);
Expand Down Expand Up @@ -503,7 +507,6 @@ fn validator() !void {

logger.infof("accounts-db setup done...", .{});

gossip_handle.join();
repair_handle.join();
shred_receive_handle.join();
}
Expand Down Expand Up @@ -570,7 +573,7 @@ fn initRepair(
};
}

fn runGossipWithConfigValues(gossip_service: *GossipService) !void {
fn runGossipWithConfigValues(gossip_service: *GossipService) !*GossipService.RunState {
const gossip_config = config.current.gossip;
return gossip_service.run(gossip_config.spy_node, gossip_config.dump);
}
Expand Down Expand Up @@ -736,8 +739,10 @@ fn downloadSnapshot() !void {
&.{},
);
defer gossip_service.deinit();
const handle = try std.Thread.spawn(.{}, runGossipWithConfigValues, .{&gossip_service});
handle.detach();

const gossip_run_state = try runGossipWithConfigValues(&gossip_service);
_ = &gossip_run_state;
// defer gossip_run_state.deinit(&gossip_service);

const trusted_validators = try getTrustedValidators(gpa_allocator);
defer if (trusted_validators) |*tvs| tvs.deinit();
Expand Down
10 changes: 6 additions & 4 deletions src/gossip/fuzz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,13 @@ pub fn run() !void {
&fuzz_exit,
.noop,
);
defer {
fuzz_exit.store(true, .unordered);
gossip_service_fuzzer.deinit();
}

var fuzz_handle = try std.Thread.spawn(.{}, GossipService.run, .{ &gossip_service_fuzzer, true, false });
const gossip_run_state = try gossip_service_fuzzer.run(true, false);
defer gossip_run_state.deinit(&gossip_service_fuzzer);

const SLEEP_TIME = 0;
// const SLEEP_TIME = std.time.ns_per_ms * 10;
Expand Down Expand Up @@ -403,9 +408,6 @@ pub fn run() !void {

// cleanup
std.debug.print("\t=> shutting down...\n", .{});
fuzz_exit.store(true, .unordered);
fuzz_handle.join();
gossip_service_fuzzer.deinit();
std.debug.print("\t=>fuzzy gossip service shutdown\n", .{});

exit_handle.join();
Expand Down
Loading

0 comments on commit fcf40c7

Please sign in to comment.