Skip to content

Commit

Permalink
Return Err when malloc fails
Browse files Browse the repository at this point in the history
  • Loading branch information
icppWorld committed Mar 29, 2024
1 parent ffa1d1f commit 9a2190b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
15 changes: 11 additions & 4 deletions icpp_llama2/src/chats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void init_run_state(RunState *s) {
}

// key = principal or ordinal-id
void build_new_chat(std::string key) {
bool build_new_chat(std::string key, IC_API &ic_api) {
// --------------------------------------------------------------------------
// Create entries for this key in the persisted memory, if not yet done
if (p_chats && p_chats->umap.find(key) == p_chats->umap.end()) {
Expand Down Expand Up @@ -115,7 +115,12 @@ void build_new_chat(std::string key) {
// Reset the Chat data
Chat *chat = &p_chats->umap[key];
free_run_state(&chat->state);
malloc_run_state(&chat->state, &transformer.config);
if (!malloc_run_state(&chat->state, &transformer.config)) {
std::string error_msg = "malloc_run_state failed";
ic_api.to_wire(CandidTypeVariant{
"Err", CandidTypeVariant{"Other", CandidTypeText{error_msg}}});
return false;
}

// Reset the output data
std::string *output_history = &p_chats_output_history->umap[key];
Expand All @@ -138,9 +143,11 @@ void build_new_chat(std::string key) {
MetadataChat metadata_chat;
metadata_chat.start_time = IC_API::time(); // time in ns
metadata_user->metadata_chats.push_back(metadata_chat);

return true;
}

bool is_ready_and_authorized(IC_API ic_api) {
bool is_ready_and_authorized(IC_API &ic_api) {

if (!ready_for_inference) {
uint16_t status_code = Http::StatusCode::InternalServerError;
Expand Down Expand Up @@ -176,7 +183,7 @@ void new_chat() {
CandidTypePrincipal caller = ic_api.get_caller();
std::string principal = caller.get_text();

build_new_chat(principal);
if (!build_new_chat(principal, ic_api)) return;

CandidTypeRecord status_code_record;
status_code_record.append("status_code",
Expand Down
4 changes: 2 additions & 2 deletions icpp_llama2/src/chats.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ void new_p_chats();
void delete_p_chats();
void new_p_metadata_users();
void delete_p_metadata_users();
void build_new_chat(std::string key);
bool is_ready_and_authorized(IC_API ic_api);
bool build_new_chat(std::string key, IC_API &ic_api);
bool is_ready_and_authorized(IC_API &ic_api);
2 changes: 1 addition & 1 deletion icpp_llama2/src/inference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void inference() {
std::string principal = caller.get_text();

if (p_chats && p_chats->umap.find(principal) == p_chats->umap.end()) {
build_new_chat(principal);
if (!build_new_chat(principal, ic_api)) return;
}
if (!p_chats || !p_chats_output_history) {
std::string error_msg =
Expand Down
2 changes: 1 addition & 1 deletion icpp_llama2/src/nft_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ void nft_story_(bool story_start) {
if (story_start or
(p_chats && p_chats->umap.find(token_id) == p_chats->umap.end())) {
// Does not yet exist
build_new_chat(token_id);
if (!build_new_chat(token_id, ic_api)) return;
}
Chat *chat = &p_chats->umap[token_id];
std::string *output_history = &p_chats_output_history->umap[token_id];
Expand Down
2 changes: 1 addition & 1 deletion icpp_llama2/src/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool malloc_run_state(RunState* s, Config* p) {
// ICPP: The calling function will trap the canister with a message
// printf("malloc failed!\n");
// exit(1);
return false; // TODO: no trap! Return error code ?
return false; // ICPP: caller with return Err
}
return true; // ICPP: all is ok
}
Expand Down

0 comments on commit 9a2190b

Please sign in to comment.