Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This project _loosely_ adheres to [Semantic Versioning](https://semver.org/spec/
- Fix handling of wrong passphrase during unlock (was frozen) [PR#596](https://github.com/coasys/ad4m/pull/596)
- Fix open buttons not working on Linux by using Tauri's opener plugin [PR#599](https://github.com/coasys/ad4m/pull/599)
- Harden setup of query subscriptions with delayed init handshakes in all cases and resubscribing after timeout [PR#601](https://github.com/coasys/ad4m/pull/601)
- Fix crash when default ports are used by other processes [PR#602](https://github.com/coasys/ad4m/pull/602)

### Added
- Prolog predicates needed in new Flux mention notification trigger:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 16 additions & 7 deletions executor/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,23 @@ export interface SeedFileSchema {
export async function init(config: OuterConfig): Promise<Ad4mCore> {
let {
appDataPath, networkBootstrapSeed, appLangAliases, bootstrapFixtures, languageLanguageOnly,
mocks, gqlPort, adminCredential, runDappServer,
dAppPort, logHolochainMetrics
adminCredential, logHolochainMetrics
} = config
if(!gqlPort) gqlPort = 4000
// Check to see if PORT 2000 & 1337 are available if not returns a random PORT
if(!config.hcPortAdmin) config.hcPortAdmin = await getPort({ port: 2000 });
if(!config.hcPortApp) config.hcPortApp = await getPort({ port: 1337 });
if(!dAppPort) dAppPort = await getPort({port: 4200})

// Moved the port check to Rust
// BUT: we have weird problem with our JS runtime
// (which will be refactored soon, when we move over the last remaining JS code to Rust)
// when this function doesn't actually do some async I/O operations, the JS event loop doesn't
// seem to work for future JS calls.
// So this here is a hack that works for now.
// Putting it in a try/catch block to avoid the process from crashing if the port is already in use.
try {
await getPort({ port: 50000 })
} catch (error) {
//ignore
}

//await new Promise(resolve => setTimeout(resolve, 1000));
if(config.hcUseMdns === undefined) config.hcUseMdns = false
if(config.hcUseProxy === undefined) config.hcUseProxy = true
if(config.hcUseBootstrap === undefined) config.hcUseBootstrap = true
Expand Down
1 change: 1 addition & 0 deletions rust-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ rodio = "*"
libc = "0.2"
chat-gpt-lib-rs = { version = "0.5.1", git = "https://github.com/coasys/chat-gpt-lib-rs" }
anyhow = "1.0.95"
portpicker = "0.1.1"

[dev-dependencies]
maplit = "1.0.2"
Expand Down
2 changes: 1 addition & 1 deletion rust-executor/src/js_core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl JsCore {
.execute_async_smart(format!("initCore({})", config.get_json()))
.await
.expect("to be able to create js execution future")
.await ;
.await;

match result {
Ok(res) => {
Expand Down
21 changes: 19 additions & 2 deletions rust-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod holochain_service;
mod js_core;
mod prolog_service;
mod runtime_service;
mod utils;
pub mod utils;
mod wallet;

pub mod agent;
Expand All @@ -34,7 +34,7 @@ use js_core::JsCore;
use crate::{
agent::AgentService, ai_service::AIService, dapp_server::serve_dapp, db::Ad4mDb,
languages::LanguageController, prolog_service::init_prolog_service,
runtime_service::RuntimeService,
runtime_service::RuntimeService, utils::find_port,
};
pub use config::Ad4mConfig;
pub use holochain_service::run_local_hc_services;
Expand All @@ -45,6 +45,19 @@ extern "C" fn handle_sigurg(_: libc::c_int) {
//println!("Received SIGURG signal, but ignoring it.");
}

fn find_and_set_port(config_port: &mut Option<u16>, start_port: u16, service_name: &str) {
if config_port.is_none() {
match find_port(start_port, 40000) {
Ok(port) => *config_port = Some(port),
Err(e) => {
let error_string = format!("Failed to find port for {}: {}", service_name, e);
error!("{}", error_string);
panic!("{}", error_string);
}
}
}
}

/// Runs the GraphQL server and the deno core runtime
pub async fn run(mut config: Ad4mConfig) -> JoinHandle<()> {
unsafe {
Expand Down Expand Up @@ -124,6 +137,10 @@ pub async fn run(mut config: Ad4mConfig) -> JoinHandle<()> {
info!("Initializing Prolog service...");
init_prolog_service().await;

find_and_set_port(&mut config.gql_port, 4000, "GraphQL");
find_and_set_port(&mut config.hc_admin_port, 2000, "Holochain admin");
find_and_set_port(&mut config.hc_app_port, 1337, "Holochain app");

info!("Starting js_core...");
let mut js_core_handle = JsCore::start(config.clone()).await;
js_core_handle.initialized().await;
Expand Down
14 changes: 14 additions & 0 deletions rust-executor/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
use dirs::home_dir;
use portpicker;
use std::path::PathBuf;

pub(crate) fn ad4m_data_directory() -> PathBuf {
home_dir().unwrap().join(".ad4m")
}

pub fn find_port(start_port: u16, end_port: u16) -> Result<u16, String> {
for x in start_port..end_port {
if portpicker::is_free(x) {
return Ok(x);
}
}

Err(format!(
"No open port found between: [{:?}, {:?}]",
start_port, end_port
))
}
1 change: 0 additions & 1 deletion ui/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ serde = { version = "1.0", features = ["derive"] }
log = "0.4"
log4rs = "1.0.0"
nix = "0.23.1"
portpicker = "0.1.1"
libc = "0.2"
directories = "4.0.1"
opener = "0.5.0"
Expand Down
8 changes: 6 additions & 2 deletions ui/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use colored::Colorize;
use libc::{rlimit, setrlimit, RLIMIT_NOFILE};
use log::LevelFilter;
use log::{debug, error, info};
use rust_executor::utils::find_port;
use rust_executor::Ad4mConfig;
use std::env;
use std::fs;
Expand Down Expand Up @@ -49,7 +50,6 @@ use crate::commands::state::{get_port, request_credential};
use crate::config::log_path;

use crate::menu::reveal_log_file;
use crate::util::find_port;
use crate::util::{create_main_window, save_executor_port};
use tauri::Manager;

Expand Down Expand Up @@ -187,7 +187,11 @@ pub fn run() {

tracing::subscriber::set_global_default(subscriber).expect("Failed to set tracing subscriber");

let free_port = find_port(12000, 13000);
let free_port = find_port(12000, 13000).unwrap_or_else(|e| {
let error_string = format!("Failed to find free main executor interface port: {}", e);
error!("{}", error_string);
panic!("{}", error_string);
});

info!("Free port: {:?}", free_port);

Expand Down
13 changes: 0 additions & 13 deletions ui/src-tauri/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ use tauri::{AppHandle, Manager, WebviewUrl, WebviewWindowBuilder, Wry};
use tauri_plugin_positioner::Position;
use tauri_plugin_positioner::WindowExt;

pub fn find_port(start_port: u16, end_port: u16) -> u16 {
for x in start_port..end_port {
if portpicker::is_free(x) {
return x;
}
}

panic!(
"No open port found between: [{:?}, {:?}]",
start_port, end_port
);
}

pub fn _has_processes_running(name: &str) -> usize {
let processes = System::new_all();
let processes_by_name: Vec<&Process> = processes.processes_by_exact_name(name).collect();
Expand Down