Skip to content

Commit

Permalink
fix NPE when getting native thread id (#15301) (#15322)
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)

Co-authored-by: kaisecheng <[email protected]>
  • Loading branch information
github-actions[bot] and kaisecheng authored Sep 14, 2023
1 parent 449b771 commit b5d03c5
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 b5d03c5

Please sign in to comment.