Skip to content

Commit

Permalink
fix NPE when getting native thread id (#15301)
Browse files Browse the repository at this point in the history
This commit adds null guard to get the native thread when constructing pipeline report

Fix: #15300
(cherry picked from commit cd78558)
  • Loading branch information
kaisecheng authored and logstashmachine committed Sep 14, 2023
1 parent 449b771 commit 8a1f3ed
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.logstash.config.ir.compiler.AbstractOutputDelegatorExt;

import java.util.Collection;
import java.util.Optional;

/**
* JRuby extension
Expand Down Expand Up @@ -161,7 +162,7 @@ private RubyArray workerStates(final ThreadContext context, final RubyHash batch
final RubyArray result = context.runtime.newArray();
((Iterable<IRubyObject>) pipeline.callMethod(context, "worker_threads"))
.forEach(thread -> {
final long nativeThreadId = ((RubyThread) thread).getNativeThread().getId();

final RubyHash hash = RubyHash.newHash(context.runtime);
IRubyObject status = thread.callMethod(context, "status");
if (status.isNil()) {
Expand All @@ -170,8 +171,15 @@ private RubyArray workerStates(final ThreadContext context, final RubyHash batch
hash.op_aset(context, STATUS_KEY, status);
hash.op_aset(context, ALIVE_KEY, thread.callMethod(context, "alive?"));
hash.op_aset(context, INDEX_KEY, context.runtime.newFixnum(result.size()));
final IRubyObject batch = batchMap.op_aref(context, context.runtime.newFixnum(nativeThreadId));
hash.op_aset(context, INFLIGHT_COUNT_KEY, extractBatchSize(context, batch));

IRubyObject batchSize = Optional.of((RubyThread) thread)
.map(RubyThread::getNativeThread)
.map(Thread::getId)
.map(id -> batchMap.op_aref(context, context.runtime.newFixnum(id)))
.map(batch -> extractBatchSize(context, batch))
.orElse(context.runtime.newFixnum(0L));

hash.op_aset(context, INFLIGHT_COUNT_KEY, batchSize);
result.add(hash);
});
return result;
Expand Down

0 comments on commit 8a1f3ed

Please sign in to comment.