Skip to content

Segfaults in C++ native extension #74

@zakjan

Description

@zakjan

Opening this issue to summarize the state of C++ native extension stability.

During the initial implementation of C++ native parser, there used to be frequent segfaults. In that time, I decreased segfault frequency to none on my machine by reordering of Ruby code, so that it always first extracts values from native classes to Ruby variables, and only then uses them for further processing. It's strange, but it helps, as if it introduces some sort of a sync barrier (if we were in a multi-threaded code, but we're not). It might be caused by Rice or Ruby GC, but I couldn't find the real cause yet.

def visit_schema_decl(ctx)
ctx__schema_id = ctx.schema_id
ctx__schema_version_id = ctx.schema_version_id
ctx__schema_body = ctx.schema_body
ctx__schema_body__interface_specification = ctx__schema_body&.interface_specification
ctx__schema_body__constant_decl = ctx__schema_body&.constant_decl
ctx__schema_body__schema_body_declaration = ctx__schema_body&.schema_body_declaration
id = visit_if(ctx__schema_id)
version = visit_if(ctx__schema_version_id)
interfaces = visit_if_map(ctx__schema_body__interface_specification)
constants = visit_if(ctx__schema_body__constant_decl, [])
declarations = visit_if_map(ctx__schema_body__schema_body_declaration)
types = declarations.select{|x| x.is_a? Model::Type}
entities = declarations.select{|x| x.is_a? Model::Entity}
subtype_constraints = declarations.select{|x| x.is_a? Model::SubtypeConstraint}
functions = declarations.select{|x| x.is_a? Model::Function}
rules = declarations.select{|x| x.is_a? Model::Rule}
procedures = declarations.select{|x| x.is_a? Model::Procedure}
Model::Schema.new({
id: id,
version: version,
interfaces: interfaces,
constants: constants,
types: types,
entities: entities,
subtype_constraints: subtype_constraints,
functions: functions,
rules: rules,
procedures: procedures
})
end

There is a note to avoid retaining any references to ANTLR4 native classes in antlr4-native-rb, otherwise it leads to segfaults. I think that I'm following this correctly, but apparently there is more to that. In a discussion with the author, he's also not sure about the real cause.

Based on https://github.com/metanorma/annotated-express/pull/70#issuecomment-800842128 segfaults still occur. Rarely, but they do. This needs more investigation. If a segfault occurs, currently just re-run the command.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions