View reduce function not working as expected

I have following reduce function:

function(key, values, rereduce) {
var s=[];
for(i=0; i < values.length; i++) {
s = values[i];

}
return s[12];
}
i have normally 1 to 5 elements in values. each element is an array with 14 fields.

When i fire query (On UI), it throws following error:
error (Reducer: Error building index for view temp, reason: TypeError: Cannot read property ‘12’ of null)

when i wrap return value in array, like
return [s[12]];

it works and returns correct output.

Also, when i return entire values as is, indexer goes out of memory.indexing runs forever.

function(key, values, rereduce) {
return values;
}

I can see following messages in dmesg output

[10549.396678] Out of memory: Kill process 12289 (couch_view_inde) score 632 or sacrifice child
[10549.396687] Killed process 12289 (couch_view_inde) total-vm:4227088kB, anon-rss:3535272kB, file-rss:948kB

What does this all mean ? I am totally confused by this behavior.
can you please explain what is wrong here ?

Could you please suggest work-around (if any) to get rid of this ?

Is this related to https://issues.couchbase.com/browse/MB-14602

I think i found the issue. The problem was that i was not handling re-reduce properly.

Glad you found it! Yes, re-reduce needs to be carefully written to handle incremental updates with existing reduced data.