Skip to content

Commit 19fea87

Browse files
committed
Merge pull request atomvm#511 from pguyot-w16/add-missing-memory_ensure_free
Fix handling of exit reason in non-SMP builds This problem is solved differently in feature/smp branch. The exit reason was in ctx instead of target here, which creates a problem when target runs the garbage collector as the exit reason will be copied but will not be accounted for in the used memory, thus a future memory_ensure_free may fail if the exit_reason is larger than `MIN_FREE_SPACE_SIZE`. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 7b5b409 + 0050509 commit 19fea87

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/libAtomVM/context.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,13 @@ static void context_monitors_handle_terminate(Context *ctx)
187187

188188
mailbox_send(target, info_tuple);
189189
} else {
190-
target->exit_reason = memory_copy_term_tree(&ctx->heap_ptr, ctx->exit_reason, &ctx->mso_list);
190+
size_t estimated_mem_usage = memory_estimate_usage(ctx->exit_reason);
191+
if (UNLIKELY(memory_ensure_free(target, estimated_mem_usage) != MEMORY_GC_OK)) {
192+
//TODO: handle out of memory here
193+
fprintf(stderr, "Cannot handle out of memory.\n");
194+
AVM_ABORT();
195+
}
196+
target->exit_reason = memory_copy_term_tree(&target->heap_ptr, ctx->exit_reason, &target->mso_list);
191197

192198
// TODO: this cannot work on multicore systems
193199
// target context should be marked as killed and terminated during next scheduling

0 commit comments

Comments
 (0)