问:
Spring @Scheduled timer accuracy
I'm using spring-boot @Scheduled annotation with fixedDelay in milliseconds as documented in javadoc:

Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.

Code:

```<code>@Scheduled(fixedDelay=1000)
public void task() {
    LOG.info("START: " + System.currentTimeInMillis());
    ....do some work here...
    LOG.info("END: " + System.currentTimeInMillis());
}
</code>```

And sometimes I get such output that time between previous task end and next task starts is less than 1000ms for about 2-30 milliseconds.

Is it normal due to some granularity or why is it happening? Is there any guaranties about this delta value?

答:
There are different ways in which you can use @Scheduled annotation.

According to the documentation:

The fixedRate invokes the method every t ms but the time delay is measured from the start of the invocation. If t ms are passed and the method is still in execution then the next invocation will wait for it to finish and will invoke right after the first one. Try putting Thread.sleep(3000) in your method. I think that your method is taking about 950ms to complete.

If you want to wait after finishing the execution you can use fixedDelay.

Title: Spring @Scheduled timer accuracy

Body:
I'm using spring-boot @Scheduled annotation with fixedDelay in milliseconds as documented in javadoc:

Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.

Code:

@Scheduled(fixedDelay=1000)
public void task() {
    LOG.info("START: " + System.currentTimeInMillis());
    ....do some work here...
    LOG.info("END: " + System.currentTimeInMillis());
}

And sometimes I get such output that time between previous task end and next task starts is less than 1000ms for about 2-30 milliseconds.

Is it normal due to some granularity or why is it happening? Is there any guaranties about this delta value?


来源: stackexchange

问:
Does shuffling happens before reduce function is called in the reducer task?
eg in a word count job,
I have 2 mappers Mapper A and Mapper B.

The output of mapper A is: {hi,1},{hello,1},{hey,1}

The output of mapper B is: {hi,1},{bye,1},{hey,1}

Suppose, there is no combiner and 1 reducer

then, first, shuffling happens

so, in shuffling the output of both the mappers merge and the resultant is:

```<code>{hi,[1,1]},{hello,1},{hey,[1,1]},{bye,1}
</code>```

then sorting happens:

```<code>{bye,1},{hello,1},{hey,[1,1]},{hi,[1,1]}
</code>```

then the reduce function in the reducer task is called which makes the o/p as:

```<code>bye,2
hello,1
hey,2
hi,2
</code>```

Is the above process correct? And does shuffling happens before reduce function is called? or the scheduler just accumulates data together from different mappers but do not group the data with the same key together and this process happens after sorting? Why is sorting useful here?

答:
The short answer is: yes, shuffling happens before reduce() is called. Sorting is needed to help reducer group values by key.

For more details you can check out the answer here: What is the purpose of shuffling and sorting phase in the reducer in Map Reduce Programming?

ContentLicense: CC BY-SA 3.0

Body:
eg in a word count job,
I have 2 mappers Mapper A and Mapper B.

The output of mapper A is: {hi,1},{hello,1},{hey,1}

The output of mapper B is: {hi,1},{bye,1},{hey,1}

Suppose, there is no combiner and 1 reducer

then, first, shuffling happens

so, in shuffling the output of both the mappers merge and the resultant is:

{hi,[1,1]},{hello,1},{hey,[1,1]},{bye,1}

then sorting happens:

{bye,1},{hello,1},{hey,[1,1]},{hi,[1,1]}

then the reduce function in the reducer task is called which makes the o/p as:

bye,2
hello,1
hey,2
hi,2

Is the above process correct? And does shuffling happens before reduce function is called? or the scheduler just accumulates data together from different mappers but do not group the data with the same key together and this process happens after sorting? Why is sorting useful here?


问:
Android application crashes randomly after using JNI
I have an application which uses a C library through JNI bindings.
The same C library is used on iOS without any issues, and it was used for around 2 years already.

I've tried implementing the same library on Android, and in general, everything works as expected, but the app crashes randomly after calling the JNI code.

What I mean is that I call the library, I receive data, I do some things with the data [like update buttons on screen] and then wait for further user input.
After around 200-300ms after the call, my app crashes. By random, I mean that 1 out of 3 times it crashes. Sometimes after the first call, sometimes after 4th etc. All the time I use the same set of data, so this issue is not data specific.

Unfortunately, the errors I get are random as well. This leads me to believe, that it must be something with memory management, but damn... I have no idea how to debug it at all.

Here are few examples of the errors I get.

```<code>                                                                 ----

----- beginning of crash
06-03 13:35:37.823 6035-6035/com.mycompany.myapp A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x30384636 in tid 6035 (mycompany.myapp)

                                                             [ 06-03 13:35:37.829   182:  182 W/         ]
                                                             debuggerd: handling request: pid=6035 uid=10147 gid=10147 tid=6035
06-03 13:35:37.919 6307-6307/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-03 13:35:37.919 6307-6307/? A/DEBUG: Build fingerprint: 'nvidia/wx_un_do/shieldtablet:7.0/NRD90M/1928188_850.6612:user/release-keys'
06-03 13:35:37.919 6307-6307/? A/DEBUG: Revision: '0'
06-03 13:35:37.919 6307-6307/? A/DEBUG: ABI: 'arm'
06-03 13:35:37.920 6307-6307/? A/DEBUG: pid: 6035, tid: 6035, name: mycompany.myapp  >>> com.mycompany.myapp <<<
06-03 13:35:37.920 6307-6307/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x30384636
06-03 13:35:37.920 6307-6307/? A/DEBUG:     r0 30384636  r1 00000058  r2 a49016e8  r3 00000058
06-03 13:35:37.920 6307-6307/? A/DEBUG:     r4 a480628c  r5 3038392e  r6 00000000  r7 a4806278
06-03 13:35:37.920 6307-6307/? A/DEBUG:     r8 a53532e8  r9 00000001  sl a53532dc  fp 00000001
06-03 13:35:37.920 6307-6307/? A/DEBUG:     ip a5349860  sp bea94a68  lr a532f97d  pc a5308b26  cpsr 200f0030
06-03 13:35:37.991 6307-6307/? A/DEBUG: backtrace:
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #00 pc 00047b26  /system/lib/libc.so (pthread_mutex_lock+1)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #01 pc 0006e979  /system/lib/libc.so (je_tcache_bin_flush_small+144)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #02 pc 0006e88d  /system/lib/libc.so (je_tcache_event_hard+60)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #03 pc 00064d09  /system/lib/libc.so (je_calloc+776)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #04 pc 0000f94d  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_malloc+64)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #05 pc 0000eeab  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_get_name+502)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #06 pc 0000ef8f  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_get_nodes+42)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #07 pc 0000c079  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (get_attr_content+12)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #08 pc 0000c785  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (get_prodcore_item+92)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #09 pc 0000c82f  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (get_prodcore_items+98)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #10 pc 0000c8d3  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (get_prod_prodcore+54)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #11 pc 0000a1ab  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (parse_prod_file_from_buf+70)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #12 pc 0000acf7  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (mylib_get_basic_info+238)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #13 pc 0000b611  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (mylib_open+388)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #14 pc 0000779d  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (Java_com_mycompany_mylib_mylibJNI_openmylibprod+60)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #15 pc 000ad919  /system/lib/libart.so (art_quick_generic_jni_trampoline+40)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #16 pc 000a8f41  /system/lib/libart.so (art_quick_invoke_stub_internal+64)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #17 pc 0040535d  /system/lib/libart.so (art_quick_invoke_stub+232)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #18 pc 000b0209  /system/lib/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+136)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #19 pc 001ec4eb  /system/lib/libart.so (_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+198)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #20 pc 001e6a8b  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+478)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #21 pc 0020d1d3  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+422)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #22 pc 00203259  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #23 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #24 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #25 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #26 pc 0020d1d3  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+422)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #27 pc 00203259  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #28 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #29 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #30 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #31 pc 0020e809  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE1ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+264)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #32 pc 002053c1  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+21456)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #33 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #34 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #35 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #36 pc 0020e809  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE1ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+264)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #37 pc 002053c1  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+21456)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #38 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #39 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #40 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #41 pc 0020f641  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE0ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+232)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #42 pc 00203ffd  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+16396)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #43 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #44 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #45 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #46 pc 0020f033  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE4ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+830)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #47 pc 00205c45  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+23636)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #48 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #49 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #50 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #51 pc 0020d1d3  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+422)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #52 pc 00203259  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #53 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #54 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #55 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #56 pc 0020f033  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE4ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+830)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #57 pc 00205c45  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+23636)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #58 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #59 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #60 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #61 pc 0020d1d3  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+422)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #62 pc 00203259  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #63 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
</code>```

another one:

```<code>06-03 14:06:34.775 8746-8746/com.mycompany.myapp A/libc: Fatal signal 11 (SIGSEGV), code 2, fault addr 0x3438379e in tid 8746 (mycompany.myapp)

                                                             [ 06-03 14:06:34.776   182:  182 W/         ]
                                                             debuggerd: handling request: pid=8746 uid=10147 gid=10147 tid=8746
06-03 14:06:34.842 9224-9224/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-03 14:06:34.843 9224-9224/? A/DEBUG: Build fingerprint: 'nvidia/wx_un_do/shieldtablet:7.0/NRD90M/1928188_850.6612:user/release-keys'
06-03 14:06:34.843 9224-9224/? A/DEBUG: Revision: '0'
06-03 14:06:34.843 9224-9224/? A/DEBUG: ABI: 'arm'
06-03 14:06:34.843 9224-9224/? A/DEBUG: pid: 8746, tid: 8746, name: mycompany.myapp  >>> com.mycompany.myapp <<<
06-03 14:06:34.843 9224-9224/? A/DEBUG: signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x3438379e
06-03 14:06:34.843 9224-9224/? A/DEBUG:     r0 3438379e  r1 00000058  r2 a4900e50  r3 00000058
06-03 14:06:34.843 9224-9224/? A/DEBUG:     r4 a4806034  r5 3438332e  r6 00000000  r7 a4806020
06-03 14:06:34.843 9224-9224/? A/DEBUG:     r8 a53532e8  r9 00000004  sl a53532dc  fp 00000004
06-03 14:06:34.843 9224-9224/? A/DEBUG:     ip a5349860  sp bea948c8  lr a532f97d  pc a5308b26  cpsr 20070030
06-03 14:06:34.945 9224-9224/? A/DEBUG: backtrace:
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #00 pc 00047b26  /system/lib/libc.so (pthread_mutex_lock+1)
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #01 pc 0006e979  /system/lib/libc.so (je_tcache_bin_flush_small+144)
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #02 pc 00066f09  /system/lib/libc.so (ifree+432)
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #03 pc 000671eb  /system/lib/libc.so (je_free+74)
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #04 pc 00010c2f  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_del_from_pool+72)
</code>```

and yet another:

```<code>06-03 21:23:53.136 19575-19829/com.mycompany.myapp A/libc: Fatal signal 11 (SIGSEGV), code 2, fault addr 0x3438480a in tid 19829 (RenderThread)

                                                               [ 06-03 21:23:53.137   182:  182 W/         ]
                                                               debuggerd: handling request: pid=19575 uid=10147 gid=10147 tid=19829
06-03 21:23:53.446 19995-19995/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-03 21:23:53.446 19995-19995/? A/DEBUG: Build fingerprint: 'nvidia/wx_un_do/shieldtablet:7.0/NRD90M/1928188_850.6612:user/release-keys'
06-03 21:23:53.447 19995-19995/? A/DEBUG: Revision: '0'
06-03 21:23:53.447 19995-19995/? A/DEBUG: ABI: 'arm'
06-03 21:23:53.448 19995-19995/? A/DEBUG: pid: 19575, tid: 19829, name: RenderThread  >>> com.mycompany.myapp <<<
06-03 21:23:53.448 19995-19995/? A/DEBUG: signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x3438480a
06-03 21:23:53.448 19995-19995/? A/DEBUG:     r0 3438392e  r1 8b8fda20  r2 00000001  r3 00000000
06-03 21:23:53.449 19995-19995/? A/DEBUG:     r4 a483aa00  r5 8b8fd3ec  r6 98356000  r7 00000002
06-03 21:23:53.449 19995-19995/? A/DEBUG:     r8 00000001  r9 00000000  sl a4863a84  fp a4863a80
06-03 21:23:53.449 19995-19995/? A/DEBUG:     ip a71ecf18  sp 8b8fd330  lr 98305edf  pc 98305eec  cpsr 00000030
06-03 21:23:53.481 19995-19995/? A/DEBUG: backtrace:
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #00 pc 0000deec  /system/vendor/lib/egl/libEGL_tegra.so
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #01 pc 00013805  /system/vendor/lib/egl/libEGL_tegra.so (eglGetError+4)
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #02 pc 0000c071  /system/lib/libEGL.so (eglGetError+20)
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #03 pc 0000b93f  /system/lib/libEGL.so (eglQuerySurface+22)
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #04 pc 00025963  /system/lib/libhwui.so
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #05 pc 0002311f  /system/lib/libhwui.so
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #06 pc 00024d6b  /system/lib/libhwui.so
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #07 pc 00028181  /system/lib/libhwui.so (_ZN7android10uirenderer12renderthread12RenderThread10threadLoopEv+80)
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #08 pc 0000f155  /system/lib/libutils.so (_ZN7android6Thread11_threadLoopEPv+144)
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #09 pc 00065e75  /system/lib/libandroid_runtime.so (_ZN7android14AndroidRuntime15javaThreadShellEPv+80)
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #10 pc 00047543  /system/lib/libc.so (_ZL15__pthread_startPv+22)
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #11 pc 0001a115  /system/lib/libc.so (__start_thread+6)
</code>```

I have to admit, I hate this kind of errors. I have no clue where to look for solutions.

Now a bit about the library:

it takes a zip file, unpacks an xml file out of it and parses data from the xml into a struct which then is used to generate an object with all of that data.

That's basically it.
In the JNI class I hold the value of JNIEnv because there is a scenario where I have to call java class from C, but not in this case. The calls I make are limited to the description I gave above.

Also, this happens very often on first call so I doubt it has anything to do with that.

Is there anything else I could provide to make it possible to determine the cause of the crashes?

I am a bit fed up with the issue, if it was up to me I would just rewrite the lib in java, but the whole idea of getting the C lib was to have a single code running on all platforms.

I will be grateful for any help.

EDIT
I was able to remove a lot of the code from calls to identify the simplest possible way for my app to crash.
Now I only have one function which when called will [randomly] crashe the app, and when not called, will never do this.

Here's the JNI code i call, quite simple. Maybe my bug is much simpler then I though.

```<code>jlong Java_com_mycompany_product_ProdJNI_openProdBook(JNIEnv* env, jobject this, jstring jprname) {

    // set the jvm variable so other C threads can access java virtual machine
    (*env)->GetJavaVM(env, &jvm);

    const char *prodname = (*env)->GetStringUTFChars(env, jprname, NULL);
    unsigned long ret = product_open(prodname);
    (*env)->ReleaseStringUTFChars(env, jprname, prodname);
    return ret;
}
</code>```

答:
This is not an answer, but it is too long for a comment.  There are two things that I would note.  First, your comment that "After around 200-300ms after the call, my app crashes [randomly]", the stack trace in two of the failures include your native library libmylibjni.so:

```<code>06-03 13:35:37.991 6307-6307/? A/DEBUG:     #00 pc 00047b26  /system/lib/libc.so (pthread_mutex_lock+1)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #01 pc 0006e979  /system/lib/libc.so (je_tcache_bin_flush_small+144)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #02 pc 0006e88d  /system/lib/libc.so (je_tcache_event_hard+60)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #03 pc 00064d09  /system/lib/libc.so (je_calloc+776)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #04 pc 0000f94d  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_malloc+64)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #05 pc 0000eeab  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_get_name+502)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #06 pc 0000ef8f  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_get_nodes+42)
</code>```

Presumably this is running from a native thread started by libart.so as you see all the way at the bottom of the stack:

```<code>06-03 13:35:37.993 6307-6307/? A/DEBUG:     #63 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
</code>```

While you have not provided us with any of your code, I'm going to hazard a theory and say that you are exchanging memory buffers between C++ and Java, and one of them is corrupting that memory, or failing to abide by mutex control over shared data (which can lead to corruption).  Hence the crash during heap operations or otherwise-unrelated code.

A second theory is that libart.so has a bug and that is causing your troubles.

If you have some code to share, we might be able to help more.

Body:
I have an application which uses a C library through JNI bindings.
The same C library is used on iOS without any issues, and it was used for around 2 years already.

I've tried implementing the same library on Android, and in general, everything works as expected, but the app crashes randomly after calling the JNI code.

What I mean is that I call the library, I receive data, I do some things with the data [like update buttons on screen] and then wait for further user input.
After around 200-300ms after the call, my app crashes. By random, I mean that 1 out of 3 times it crashes. Sometimes after the first call, sometimes after 4th etc. All the time I use the same set of data, so this issue is not data specific.

Unfortunately, the errors I get are random as well. This leads me to believe, that it must be something with memory management, but damn... I have no idea how to debug it at all.

Here are few examples of the errors I get.

                                                                 ----

----- beginning of crash
06-03 13:35:37.823 6035-6035/com.mycompany.myapp A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x30384636 in tid 6035 (mycompany.myapp)

                                                             [ 06-03 13:35:37.829   182:  182 W/         ]
                                                             debuggerd: handling request: pid=6035 uid=10147 gid=10147 tid=6035
06-03 13:35:37.919 6307-6307/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-03 13:35:37.919 6307-6307/? A/DEBUG: Build fingerprint: 'nvidia/wx_un_do/shieldtablet:7.0/NRD90M/1928188_850.6612:user/release-keys'
06-03 13:35:37.919 6307-6307/? A/DEBUG: Revision: '0'
06-03 13:35:37.919 6307-6307/? A/DEBUG: ABI: 'arm'
06-03 13:35:37.920 6307-6307/? A/DEBUG: pid: 6035, tid: 6035, name: mycompany.myapp  >>> com.mycompany.myapp <<<
06-03 13:35:37.920 6307-6307/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x30384636
06-03 13:35:37.920 6307-6307/? A/DEBUG:     r0 30384636  r1 00000058  r2 a49016e8  r3 00000058
06-03 13:35:37.920 6307-6307/? A/DEBUG:     r4 a480628c  r5 3038392e  r6 00000000  r7 a4806278
06-03 13:35:37.920 6307-6307/? A/DEBUG:     r8 a53532e8  r9 00000001  sl a53532dc  fp 00000001
06-03 13:35:37.920 6307-6307/? A/DEBUG:     ip a5349860  sp bea94a68  lr a532f97d  pc a5308b26  cpsr 200f0030
06-03 13:35:37.991 6307-6307/? A/DEBUG: backtrace:
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #00 pc 00047b26  /system/lib/libc.so (pthread_mutex_lock+1)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #01 pc 0006e979  /system/lib/libc.so (je_tcache_bin_flush_small+144)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #02 pc 0006e88d  /system/lib/libc.so (je_tcache_event_hard+60)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #03 pc 00064d09  /system/lib/libc.so (je_calloc+776)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #04 pc 0000f94d  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_malloc+64)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #05 pc 0000eeab  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_get_name+502)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #06 pc 0000ef8f  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_get_nodes+42)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #07 pc 0000c079  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (get_attr_content+12)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #08 pc 0000c785  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (get_prodcore_item+92)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #09 pc 0000c82f  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (get_prodcore_items+98)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #10 pc 0000c8d3  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (get_prod_prodcore+54)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #11 pc 0000a1ab  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (parse_prod_file_from_buf+70)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #12 pc 0000acf7  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (mylib_get_basic_info+238)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #13 pc 0000b611  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (mylib_open+388)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #14 pc 0000779d  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (Java_com_mycompany_mylib_mylibJNI_openmylibprod+60)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #15 pc 000ad919  /system/lib/libart.so (art_quick_generic_jni_trampoline+40)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #16 pc 000a8f41  /system/lib/libart.so (art_quick_invoke_stub_internal+64)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #17 pc 0040535d  /system/lib/libart.so (art_quick_invoke_stub+232)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #18 pc 000b0209  /system/lib/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+136)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #19 pc 001ec4eb  /system/lib/libart.so (_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+198)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #20 pc 001e6a8b  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+478)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #21 pc 0020d1d3  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+422)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #22 pc 00203259  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #23 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #24 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #25 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #26 pc 0020d1d3  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+422)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #27 pc 00203259  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #28 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #29 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.992 6307-6307/? A/DEBUG:     #30 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #31 pc 0020e809  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE1ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+264)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #32 pc 002053c1  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+21456)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #33 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #34 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #35 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #36 pc 0020e809  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE1ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+264)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #37 pc 002053c1  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+21456)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #38 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #39 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #40 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #41 pc 0020f641  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE0ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+232)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #42 pc 00203ffd  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+16396)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #43 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #44 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #45 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #46 pc 0020f033  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE4ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+830)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #47 pc 00205c45  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+23636)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #48 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #49 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #50 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #51 pc 0020d1d3  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+422)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #52 pc 00203259  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #53 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #54 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #55 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #56 pc 0020f033  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE4ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+830)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #57 pc 00205c45  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+23636)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #58 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #59 pc 001cf357  /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #60 pc 001e6a6f  /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #61 pc 0020d1d3  /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThmyappNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+422)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #62 pc 00203259  /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
06-03 13:35:37.993 6307-6307/? A/DEBUG:     #63 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)

another one:

06-03 14:06:34.775 8746-8746/com.mycompany.myapp A/libc: Fatal signal 11 (SIGSEGV), code 2, fault addr 0x3438379e in tid 8746 (mycompany.myapp)

                                                             [ 06-03 14:06:34.776   182:  182 W/         ]
                                                             debuggerd: handling request: pid=8746 uid=10147 gid=10147 tid=8746
06-03 14:06:34.842 9224-9224/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-03 14:06:34.843 9224-9224/? A/DEBUG: Build fingerprint: 'nvidia/wx_un_do/shieldtablet:7.0/NRD90M/1928188_850.6612:user/release-keys'
06-03 14:06:34.843 9224-9224/? A/DEBUG: Revision: '0'
06-03 14:06:34.843 9224-9224/? A/DEBUG: ABI: 'arm'
06-03 14:06:34.843 9224-9224/? A/DEBUG: pid: 8746, tid: 8746, name: mycompany.myapp  >>> com.mycompany.myapp <<<
06-03 14:06:34.843 9224-9224/? A/DEBUG: signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x3438379e
06-03 14:06:34.843 9224-9224/? A/DEBUG:     r0 3438379e  r1 00000058  r2 a4900e50  r3 00000058
06-03 14:06:34.843 9224-9224/? A/DEBUG:     r4 a4806034  r5 3438332e  r6 00000000  r7 a4806020
06-03 14:06:34.843 9224-9224/? A/DEBUG:     r8 a53532e8  r9 00000004  sl a53532dc  fp 00000004
06-03 14:06:34.843 9224-9224/? A/DEBUG:     ip a5349860  sp bea948c8  lr a532f97d  pc a5308b26  cpsr 20070030
06-03 14:06:34.945 9224-9224/? A/DEBUG: backtrace:
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #00 pc 00047b26  /system/lib/libc.so (pthread_mutex_lock+1)
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #01 pc 0006e979  /system/lib/libc.so (je_tcache_bin_flush_small+144)
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #02 pc 00066f09  /system/lib/libc.so (ifree+432)
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #03 pc 000671eb  /system/lib/libc.so (je_free+74)
06-03 14:06:34.945 9224-9224/? A/DEBUG:     #04 pc 00010c2f  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_del_from_pool+72)

and yet another:

06-03 21:23:53.136 19575-19829/com.mycompany.myapp A/libc: Fatal signal 11 (SIGSEGV), code 2, fault addr 0x3438480a in tid 19829 (RenderThread)

                                                               [ 06-03 21:23:53.137   182:  182 W/         ]
                                                               debuggerd: handling request: pid=19575 uid=10147 gid=10147 tid=19829
06-03 21:23:53.446 19995-19995/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-03 21:23:53.446 19995-19995/? A/DEBUG: Build fingerprint: 'nvidia/wx_un_do/shieldtablet:7.0/NRD90M/1928188_850.6612:user/release-keys'
06-03 21:23:53.447 19995-19995/? A/DEBUG: Revision: '0'
06-03 21:23:53.447 19995-19995/? A/DEBUG: ABI: 'arm'
06-03 21:23:53.448 19995-19995/? A/DEBUG: pid: 19575, tid: 19829, name: RenderThread  >>> com.mycompany.myapp <<<
06-03 21:23:53.448 19995-19995/? A/DEBUG: signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x3438480a
06-03 21:23:53.448 19995-19995/? A/DEBUG:     r0 3438392e  r1 8b8fda20  r2 00000001  r3 00000000
06-03 21:23:53.449 19995-19995/? A/DEBUG:     r4 a483aa00  r5 8b8fd3ec  r6 98356000  r7 00000002
06-03 21:23:53.449 19995-19995/? A/DEBUG:     r8 00000001  r9 00000000  sl a4863a84  fp a4863a80
06-03 21:23:53.449 19995-19995/? A/DEBUG:     ip a71ecf18  sp 8b8fd330  lr 98305edf  pc 98305eec  cpsr 00000030
06-03 21:23:53.481 19995-19995/? A/DEBUG: backtrace:
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #00 pc 0000deec  /system/vendor/lib/egl/libEGL_tegra.so
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #01 pc 00013805  /system/vendor/lib/egl/libEGL_tegra.so (eglGetError+4)
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #02 pc 0000c071  /system/lib/libEGL.so (eglGetError+20)
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #03 pc 0000b93f  /system/lib/libEGL.so (eglQuerySurface+22)
06-03 21:23:53.481 19995-19995/? A/DEBUG:     #04 pc 00025963  /system/lib/libhwui.so
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #05 pc 0002311f  /system/lib/libhwui.so
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #06 pc 00024d6b  /system/lib/libhwui.so
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #07 pc 00028181  /system/lib/libhwui.so (_ZN7android10uirenderer12renderthread12RenderThread10threadLoopEv+80)
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #08 pc 0000f155  /system/lib/libutils.so (_ZN7android6Thread11_threadLoopEPv+144)
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #09 pc 00065e75  /system/lib/libandroid_runtime.so (_ZN7android14AndroidRuntime15javaThreadShellEPv+80)
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #10 pc 00047543  /system/lib/libc.so (_ZL15__pthread_startPv+22)
06-03 21:23:53.482 19995-19995/? A/DEBUG:     #11 pc 0001a115  /system/lib/libc.so (__start_thread+6)

I have to admit, I hate this kind of errors. I have no clue where to look for solutions.

Now a bit about the library:

it takes a zip file, unpacks an xml file out of it and parses data from the xml into a struct which then is used to generate an object with all of that data.

That's basically it.
In the JNI class I hold the value of JNIEnv because there is a scenario where I have to call java class from C, but not in this case. The calls I make are limited to the description I gave above.

Also, this happens very often on first call so I doubt it has anything to do with that.

Is there anything else I could provide to make it possible to determine the cause of the crashes?

I am a bit fed up with the issue, if it was up to me I would just rewrite the lib in java, but the whole idea of getting the C lib was to have a single code running on all platforms.

I will be grateful for any help.

EDIT
I was able to remove a lot of the code from calls to identify the simplest possible way for my app to crash.
Now I only have one function which when called will [randomly] crashe the app, and when not called, will never do this.

Here's the JNI code i call, quite simple. Maybe my bug is much simpler then I though.

jlong Java_com_mycompany_product_ProdJNI_openProdBook(JNIEnv* env, jobject this, jstring jprname) {

    // set the jvm variable so other C threads can access java virtual machine
    (*env)->GetJavaVM(env, &jvm);

    const char *prodname = (*env)->GetStringUTFChars(env, jprname, NULL);
    unsigned long ret = product_open(prodname);
    (*env)->ReleaseStringUTFChars(env, jprname, prodname);
    return ret;
}

Body:
This is not an answer, but it is too long for a comment.  There are two things that I would note.  First, your comment that "After around 200-300ms after the call, my app crashes [randomly]", the stack trace in two of the failures include your native library libmylibjni.so:

06-03 13:35:37.991 6307-6307/? A/DEBUG:     #00 pc 00047b26  /system/lib/libc.so (pthread_mutex_lock+1)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #01 pc 0006e979  /system/lib/libc.so (je_tcache_bin_flush_small+144)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #02 pc 0006e88d  /system/lib/libc.so (je_tcache_event_hard+60)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #03 pc 00064d09  /system/lib/libc.so (je_calloc+776)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #04 pc 0000f94d  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_malloc+64)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #05 pc 0000eeab  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_get_name+502)
06-03 13:35:37.991 6307-6307/? A/DEBUG:     #06 pc 0000ef8f  /data/app/com.mycompany.myapp-2/lib/arm/libmylibjni.so (roxml_get_nodes+42)

Presumably this is running from a native thread started by libart.so as you see all the way at the bottom of the stack:

06-03 13:35:37.993 6307-6307/? A/DEBUG:     #63 pc 001ca845  /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)

While you have not provided us with any of your code, I'm going to hazard a theory and say that you are exchanging memory buffers between C++ and Java, and one of them is corrupting that memory, or failing to abide by mutex control over shared data (which can lead to corruption).  Hence the crash during heap operations or otherwise-unrelated code.

A second theory is that libart.so has a bug and that is causing your troubles.

If you have some code to share, we might be able to help more.


问:
Flask + Apache (mod_wsgi) + boto3: name 'ssl' is not defined
I'm trying to build an REST API that monitors the state of instances on AWS EC2 service. When I use my Flask application hosted on Apache I get an traceback informing that name 'ssl' is not defined.

To see what was wrong with my application I ran it on a standalone server (Flask only) using debug mode. For my surprise the application ran sucessfully.

My Flask application (flask_endpoint.py)

```<code>import json
import boto3
from flask import Flask
from flask_cors import CORS
from flask import make_response

WEBAPP = Flask(__name__)
CORS(WEBAPP)

@WEBAPP.route('/test_boto', methods=['GET'])
def handle_teste_boto():

    results = []
    ec2_res = boto3.resource('ec2')
    instances = ec2_res.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])

    for inst in instances:
        inst_dic = {'private_ip': inst.private_ip_address,
                    'instance_type': inst.instance_type,
                    'state': inst.state['Name']
                    }
        results.append(inst_dic)

    response_code = 200
    response = make_response(json.dumps(results, ensure_ascii=False), response_code)
    response.headers['Content-Type'] = 'application/json'
    return response

if __name__ == '__main__':
    WEBAPP.run(port=50000, host='0.0.0.0', debug=True)
</code>```

Output produced when running without Apache

```<code>[
    {
        "state": "running",
        "private_ip": "172.31.56.153",
        "instance_type": "t2.micro"
    }
]
</code>```

Apache log with truncated traceback

```<code>[Sat Jun 03 11:59:20.217312 2017] [wsgi:info] [pid 3094] [remote 192.168.0.115:50017] mod_wsgi (pid=3094, process='flask_test', application=''): Loading WSGI script '/var/www/siase/middleend/rest/cmd_server_wsgi.py'.
[Sat Jun 03 11:59:20.827295 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017] mod_wsgi (pid=3094): Exception occurred processing WSGI script '/var/www/siase/middleend/rest/cmd_server_wsgi.py'.
[Sat Jun 03 11:59:20.829915 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017] Traceback (most recent call last):
[Sat Jun 03 11:59:20.830014 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
[Sat Jun 03 11:59:20.830017 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     return self.wsgi_app(environ, start_response)
[Sat Jun 03 11:59:20.830022 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
[Sat Jun 03 11:59:20.830024 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     response = self.make_response(self.handle_exception(e))
[Sat Jun 03 11:59:20.830027 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
[Sat Jun 03 11:59:20.830029 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     return cors_after_request(app.make_response(f(*args, **kwargs)))

** (several lines) **

[Sat Jun 03 11:59:20.830194 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     body=body, headers=headers)
[Sat Jun 03 11:59:20.830197 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py", line 341, in _make_request
[Sat Jun 03 11:59:20.830199 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     self._validate_conn(conn)
[Sat Jun 03 11:59:20.830202 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py", line 761, in _validate_conn
[Sat Jun 03 11:59:20.830204 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     conn.connect()
[Sat Jun 03 11:59:20.830207 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/botocore/vendored/requests/packages/urllib3/connection.py", line 206, in connect
[Sat Jun 03 11:59:20.830209 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs)
[Sat Jun 03 11:59:20.830213 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/botocore/vendored/requests/packages/urllib3/util/ssl_.py", line 157, in resolve_cert_reqs
[Sat Jun 03 11:59:20.830216 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     res = getattr(ssl, candidate, None)
[Sat Jun 03 11:59:20.830230 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017] NameError: name 'ssl' is not defined
</code>```

This is my wsgi file

```<code># -*- coding: utf-8 -*-

import sys

APP_MODULE_PATH = '/var/www/siase/middleend'

if APP_MODULE_PATH not in sys.path:
    sys.path.insert(0, APP_MODULE_PATH)

from rest.cmd_server import WEBAPP as application
application.debug = True
</code>```

This is my .conf file

```<code><VirtualHost *:8080>

    ServerName localhost
    ServerAdmin administrator@localhost

    DocumentRoot /var/www/siase/middleend

    WSGIDaemonProcess flask_test user=studioapp group=www-data lang='en_US.UTF-8' locale='en_US.UTF-8' threads=20 processes=4 python-path=/var/www/anaconda3/lib/python3.5
    WSGIScriptAlias / /var/www/siase/middleend/rest/cmd_server_wsgi.py
    <Directory /var/www/siase/middleend>
        WSGIProcessGroup flask_test
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
        Options -Indexes
    </Directory>

    <Directory "/">
        Options -Indexes
    </Directory>

    LogLevel info
    ErrorLog ${APACHE_LOG_DIR}/flask_test-error.log
    CustomLog ${APACHE_LOG_DIR}/flask_test-access.log combined

</VirtualHost>
</code>```

Here follows some details of the environment that I currently using:

Apache/2.4.7 (Ubuntu)

OpenSSL 1.0.1f

mod_wsgi 4.5.5

Python 3.5.2 (Anaconda3 - 4.2.0)

Flask 0.11.1

Boto3 1.4.4

答:
If you are loading the mod_ssl module into Apache you are likely not going to be able to get it working. The problem is that Anaconda has been shipping its own SSL libraries and they aren't compatible with the operating system SSL libraries.

What happens is that Apache when it loads the mod_ssl module pulls in the operating system SSL libraries. Then in your Python web application, Anaconda can't use its own and inherits the SSL libraries pulled in by Apache mod_ssl library instead. Because they aren't compatible, setup of the ssl module in Python fails so appears as not being loaded.

The only solution would be to use mod_wsgi-express and sit it behind Apache with Apache acting only as a proxy. That way there is a proper separation between Apache and your Python web application.

Body:
I'm trying to build an REST API that monitors the state of instances on AWS EC2 service. When I use my Flask application hosted on Apache I get an traceback informing that name 'ssl' is not defined.

To see what was wrong with my application I ran it on a standalone server (Flask only) using debug mode. For my surprise the application ran sucessfully.

My Flask application (flask_endpoint.py)

import json
import boto3
from flask import Flask
from flask_cors import CORS
from flask import make_response

WEBAPP = Flask(__name__)
CORS(WEBAPP)

@WEBAPP.route('/test_boto', methods=['GET'])
def handle_teste_boto():

    results = []
    ec2_res = boto3.resource('ec2')
    instances = ec2_res.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])

    for inst in instances:
        inst_dic = {'private_ip': inst.private_ip_address,
                    'instance_type': inst.instance_type,
                    'state': inst.state['Name']
                    }
        results.append(inst_dic)

    response_code = 200
    response = make_response(json.dumps(results, ensure_ascii=False), response_code)
    response.headers['Content-Type'] = 'application/json'
    return response

if __name__ == '__main__':
    WEBAPP.run(port=50000, host='0.0.0.0', debug=True)

Output produced when running without Apache

[
    {
        "state": "running",
        "private_ip": "172.31.56.153",
        "instance_type": "t2.micro"
    }
]

Apache log with truncated traceback

[Sat Jun 03 11:59:20.217312 2017] [wsgi:info] [pid 3094] [remote 192.168.0.115:50017] mod_wsgi (pid=3094, process='flask_test', application=''): Loading WSGI script '/var/www/siase/middleend/rest/cmd_server_wsgi.py'.
[Sat Jun 03 11:59:20.827295 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017] mod_wsgi (pid=3094): Exception occurred processing WSGI script '/var/www/siase/middleend/rest/cmd_server_wsgi.py'.
[Sat Jun 03 11:59:20.829915 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017] Traceback (most recent call last):
[Sat Jun 03 11:59:20.830014 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
[Sat Jun 03 11:59:20.830017 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     return self.wsgi_app(environ, start_response)
[Sat Jun 03 11:59:20.830022 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
[Sat Jun 03 11:59:20.830024 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     response = self.make_response(self.handle_exception(e))
[Sat Jun 03 11:59:20.830027 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
[Sat Jun 03 11:59:20.830029 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     return cors_after_request(app.make_response(f(*args, **kwargs)))

** (several lines) **

[Sat Jun 03 11:59:20.830194 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     body=body, headers=headers)
[Sat Jun 03 11:59:20.830197 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py", line 341, in _make_request
[Sat Jun 03 11:59:20.830199 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     self._validate_conn(conn)
[Sat Jun 03 11:59:20.830202 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py", line 761, in _validate_conn
[Sat Jun 03 11:59:20.830204 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     conn.connect()
[Sat Jun 03 11:59:20.830207 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/botocore/vendored/requests/packages/urllib3/connection.py", line 206, in connect
[Sat Jun 03 11:59:20.830209 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs)
[Sat Jun 03 11:59:20.830213 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]   File "/var/www/anaconda3/lib/python3.5/site-packages/botocore/vendored/requests/packages/urllib3/util/ssl_.py", line 157, in resolve_cert_reqs
[Sat Jun 03 11:59:20.830216 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017]     res = getattr(ssl, candidate, None)
[Sat Jun 03 11:59:20.830230 2017] [wsgi:error] [pid 3094] [remote 192.168.0.115:50017] NameError: name 'ssl' is not defined

This is my wsgi file

# -*- coding: utf-8 -*-

import sys

APP_MODULE_PATH = '/var/www/siase/middleend'

if APP_MODULE_PATH not in sys.path:
    sys.path.insert(0, APP_MODULE_PATH)

from rest.cmd_server import WEBAPP as application
application.debug = True

This is my .conf file

<VirtualHost *:8080>

    ServerName localhost
    ServerAdmin administrator@localhost

    DocumentRoot /var/www/siase/middleend

    WSGIDaemonProcess flask_test user=studioapp group=www-data lang='en_US.UTF-8' locale='en_US.UTF-8' threads=20 processes=4 python-path=/var/www/anaconda3/lib/python3.5
    WSGIScriptAlias / /var/www/siase/middleend/rest/cmd_server_wsgi.py
    <Directory /var/www/siase/middleend>
        WSGIProcessGroup flask_test
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
        Options -Indexes
    </Directory>

    <Directory "/">
        Options -Indexes
    </Directory>

    LogLevel info
    ErrorLog ${APACHE_LOG_DIR}/flask_test-error.log
    CustomLog ${APACHE_LOG_DIR}/flask_test-access.log combined

</VirtualHost>

Here follows some details of the environment that I currently using:

Apache/2.4.7 (Ubuntu)

OpenSSL 1.0.1f

mod_wsgi 4.5.5

Python 3.5.2 (Anaconda3 - 4.2.0)

Flask 0.11.1

Boto3 1.4.4


问:
How to make script file to run several commands on several terminals in ubuntu?
I have to open three terminals and each terminal should go to three directories and need to run three files on those three terminals. Now I need  speed up my task. Beacuse of that need to do it by running a single script file.How can  I do that?

As a example I have to run those three commands on three terminals. But I need to do those thing using one single file running.

```<code>cd /home/xyz/Desktop/project/components/core;
python3 Main.py

aria2c --enable-rpc

cd /home/xyz/Desktop/project/ui;
gulp serve
</code>```

答:
Try to put your commands in one bash file like those below and run it from terminal.

```<code>gnome-terminal -e command_1
gnome-terminal -e command_2
gnome-terminal -e command_3
</code>```

Look here for more details https://askubuntu.com/questions/46627/how-can-i-make-a-script-that-opens-terminal-windows-and-executes-commands-in-the

Body:
I have to open three terminals and each terminal should go to three directories and need to run three files on those three terminals. Now I need  speed up my task. Beacuse of that need to do it by running a single script file.How can  I do that?

As a example I have to run those three commands on three terminals. But I need to do those thing using one single file running.

cd /home/xyz/Desktop/project/components/core;
python3 Main.py

aria2c --enable-rpc

cd /home/xyz/Desktop/project/ui;
gulp serve

Body:
Try to put your commands in one bash file like those below and run it from terminal.

gnome-terminal -e command_1
gnome-terminal -e command_2
gnome-terminal -e command_3

Look here for more details https://askubuntu.com/questions/46627/how-can-i-make-a-script-that-opens-terminal-windows-and-executes-commands-in-the


问:
Allow only numbers and special character(-) to be typed in a textbox
I need to Allow only numbers and special character minus "-" to be typed in a textbox, plz help me i already have a number restriction, but wanna minus sign too.

thanks in advance

```<code><script>
function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}
</script>

<input type="text" class="textfield" value="" id="extra7" name="extra7" onkeypress="return isNumber(event)" />
</code>```

答:
This regex will check for numbers and '-'. If there are other characters, they'll be replaced.

```<code>$('.input').keyup(function () {
    if (!this.value.match(/^(\d|-)+$/)) {
        this.value = this.value.replace(/[^0-9-]/g, '');
    }
});
</code>```

No jQuery version:

```<code><input type="text" class="textfield" value="" id="extra7" name="extra7" onkeyup="isNumber()" />

function isNumber() {
    var inputField = document.getElementById('extra7');
    if (!inputField.value.match(/^(\d|-)+$/)) {
        inputField.value = inputField.value.replace(/[^0-9-]/g, '');
    }
}
</code>```

Body:
I need to Allow only numbers and special character minus "-" to be typed in a textbox, plz help me i already have a number restriction, but wanna minus sign too.

thanks in advance

<input type="text" class="textfield" value="" id="extra7" name="extra7" onkeypress="return isNumber(event)" />

Body:
This regex will check for numbers and '-'. If there are other characters, they'll be replaced.

$('.input').keyup(function () {
    if (!this.value.match(/^(\d|-)+$/)) {
        this.value = this.value.replace(/[^0-9-]/g, '');
    }
});

No jQuery version:

<input type="text" class="textfield" value="" id="extra7" name="extra7" onkeyup="isNumber()" />

function isNumber() {
    var inputField = document.getElementById('extra7');
    if (!inputField.value.match(/^(\d|-)+$/)) {
        inputField.value = inputField.value.replace(/[^0-9-]/g, '');
    }
}


问:
id to title url rewrite not working
I want to convert id to title in url address bar , i found many question related this topic .i also created .htaccess file in root directory code look like

```<code><IfModule mod_rewrite.c>   RewriteEngine On     RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ ProductDetail.php?Product=$1 [QSA,L]
</IfModule>`
</code>```

But nothing work .i am new in rewrite URL, i just want to convert

    ProductDetail.php?Product=1212 To ProductDetail/Title

Thanks for your kind regards

答:
One basic rule for url rewriting is that it is not possible to process the query while rewriting it

If in your example ProductDetail/Title Title means the title of the item having the product=1212 we need to actually run the query to find the Title before even generating the url, which in turn will lead to change in the query method of your website and thus making it less efficient (Instead of using the product in the where clause you have to use a string which will replace the Title).

We can however create the transformation from ProductDetail.php?Product=1212 to ProductDetail/1212 with the following code

```<code>    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ ./ProductDetail.php?Product=$1 [QSA,L]
    </IfModule>
</code>```

Also, you need to precede the php file with a ./ in your path as php file resides within a directory. Otherwise it might generate an error or it won't work at all.

Regards

Title: id to title url rewrite not working

Body:
I want to convert id to title in url address bar , i found many question related this topic .i also created .htaccess file in root directory code look like

<IfModule mod_rewrite.c>   RewriteEngine On     RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ ProductDetail.php?Product=$1 [QSA,L]
</IfModule>`

But nothing work .i am new in rewrite URL, i just want to convert

    ProductDetail.php?Product=1212 To ProductDetail/Title

Thanks for your kind regards

Body:
One basic rule for url rewriting is that it is not possible to process the query while rewriting it

If in your example ProductDetail/Title Title means the title of the item having the product=1212 we need to actually run the query to find the Title before even generating the url, which in turn will lead to change in the query method of your website and thus making it less efficient (Instead of using the product in the where clause you have to use a string which will replace the Title).

We can however create the transformation from ProductDetail.php?Product=1212 to ProductDetail/1212 with the following code

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ ./ProductDetail.php?Product=$1 [QSA,L]
    </IfModule>

Also, you need to precede the php file with a ./ in your path as php file resides within a directory. Otherwise it might generate an error or it won't work at all.

Regards


问:
Modifying a pcap file based on packet length and other parameters
I agree that there are a lot of posts to do with editing .pcap files on this site. However, I haven't been able to find a .pcap editor that can effectively remove entries based on certain specific conditions.

For example, say I want to remove all packets of length 172 bytes after Time = 2 seconds. Is there a tool that provides this level of flexibility?

If not, is there a way I can access the .pcap file using C++ and then edit it, and save it back as a .pcap file?

答:
Solution #1

The most flexible approach is writing your own code for that kind of stuff. Reading a pcap file is fairy straightforward in most programming languages using dedicated libraries. In C++ it can be done using libpcap (see this answer)

Solution #2

If the pcap file isn't too big, you could use Wireshark. Just load the file into Wireshark, and type in the following display filter, followed by Enter: !(frame.time_relative > 2  && frame.len == 172). Then go to File -> Export Specified Packets.. and pick displayed, enter a file name, and save the result:

Solution #3

If the file size is large, but not huge, or if you need a command line solution, you can use tshark, the command line version of Wireshark. Just read in the original file (-r), apply the same display filter (-Y), and write the resulting file to a new location (-w):

Something like:

tshark -r input.pcap -Y "!(frame.time_relative > 2  && frame.len == 172)" -w output.pcap

Solution #4

The last solution I can suggest is using libtrace (github here).
However, it's a bit tricky since you need to filter on both packet properties as well as time.

One way would be using the following sequence of steps:

first split your pcap file using tracesplit into two files - A.pcap and B.pcap where A.pcap will contain packets until the 2 second mark, and B will contain the rest of the trace.

Use tracefilter to filter A.pcap so only packets that aren't 172 bytes long will remain - the filter string is len != 172

Use tracemerge to merge both the filtered A.pcap and B.pcap into a single file.


问:
version code not found in android studio canary -2

when instant run apply i am getting following in android studio 3.0 canary-2

```<code>Error:E:\PublicServer_Share\Extra\app\build\intermediates\instant-run
support\debug\slice_2\AndroidManifest.xml:2 attribute 'android:versionCode'
not found
Error:failed processing manifest
Error:E:\PublicServer_Share\Extra\app\build\intermediates\instant-run-
support\debug\slice_9\AndroidManifest.xml:2 attribute 'android:versionCode'
not found
Error:failed processing manifest
Error:Execution failed for task
':app:transformDexWithInstantRunSlicesApkForDebug'.
com.android.ide.common.process.ProcessException: Failed to execute aapt
</code>```

答: Disable Instant Run. Inside File -> Settings -> Build, Execution, Deployment. This will allow you to run your app in the Canary version.

Body:
when instant run apply i am getting following in android studio 3.0 canary-2

Error:E:\PublicServer_Share\Extra\app\build\intermediates\instant-run
support\debug\slice_2\AndroidManifest.xml:2 attribute 'android:versionCode'
not found
Error:failed processing manifest
Error:E:\PublicServer_Share\Extra\app\build\intermediates\instant-run-
support\debug\slice_9\AndroidManifest.xml:2 attribute 'android:versionCode'
not found
Error:failed processing manifest
Error:Execution failed for task
':app:transformDexWithInstantRunSlicesApkForDebug'.
com.android.ide.common.process.ProcessException: Failed to execute aapt


问:
How to initialize const member variable in a class?
```<code>#include <iostream>

using namespace std;
class T1
{
  const int t = 100;
  public:

  T1()
  {

    cout << "T1 constructor: " << t << endl;
  }
};
</code>```

When I am trying to initialize the const member variable t with 100. But it's giving me the following error:

```<code>test.cpp:21: error: ISO C++ forbids initialization of member ‘t’
test.cpp:21: error: making ‘t’ static
</code>```

How can I initialize a const value?

答:
If you don't want to make the const data member in class static, You can initialize the const data member using the constructor of the class.
For example:

```<code>class Example{
      const int x;
    public:
      Example(int n);
};

Example::Example(int n):x(n){
}
</code>```

if there are multiple const data members in class you can use the following syntax to initialize the members:

```<code>Example::Example(int n, int z):x(n),someOtherConstVariable(z){}
</code>```

Body:
#include <iostream>

using namespace std;
class T1
{
  const int t = 100;
  public:

  T1()
  {

    cout << "T1 constructor: " << t << endl;
  }
};

When I am trying to initialize the const member variable t with 100. But it's giving me the following error:

test.cpp:21: error: ISO C++ forbids initialization of member ‘t’
test.cpp:21: error: making ‘t’ static

How can I initialize a const value?

Body:
If you don't want to make the const data member in class static, You can initialize the const data member using the constructor of the class.
For example:

class Example{
      const int x;
    public:
      Example(int n);
};

Example::Example(int n):x(n){
}

if there are multiple const data members in class you can use the following syntax to initialize the members:

Example::Example(int n, int z):x(n),someOtherConstVariable(z){}


问:
copy bytes without memcpy()
It's a home work. I want to implement memcpy(). I was told memory area must not overlap. Actually I don't understand what that means, because this code works fine, but there is a possibility of memory overlap. How to prevent it?

```<code>void *mem_copy(void *dest, const void *src, unsigned int n) {
    assert((src != NULL) && (n > 0));
    int i = 0;
    char *newsrc = (char*)src;
    char *newdest = (char*)dest;
    while (i < n) {
        newdest[i] = newsrc[i];
        i++;
    }
    newdest[i]='\0';
    return newdest;
}
</code>```

答:
Actually I don't understand what doest that mean [for memory to overlap]

Consider this example:

```<code>char data[100];
memcpy(&data[5], &data[0], 95);
</code>```

From the program's point of view, the range of addresses from src to src+n must not overlap the range from dest to dest+n.

if there is possibility of memory overlap, how to prevent it?

You can make your algorithm work with or without an overlap by deciding to copy overlapping regions from the back if src has numerically lower address than dest.

Note: Since you are doing memcpy, not strcpy, forcing null termination with newdest[i]='\0' is incorrect, and needs to be removed.

Title: copy bytes without memcpy()

Body:
It's a home work. I want to implement memcpy(). I was told memory area must not overlap. Actually I don't understand what that means, because this code works fine, but there is a possibility of memory overlap. How to prevent it?

void *mem_copy(void *dest, const void *src, unsigned int n) {
    assert((src != NULL) && (n > 0));
    int i = 0;
    char *newsrc = (char*)src;
    char *newdest = (char*)dest;
    while (i < n) {
        newdest[i] = newsrc[i];
        i++;
    }
    newdest[i]='\0';
    return newdest;
}

Body:
Actually I don't understand what doest that mean [for memory to overlap]

Consider this example:

char data[100];
memcpy(&data[5], &data[0], 95);

From the program's point of view, the range of addresses from src to src+n must not overlap the range from dest to dest+n.

if there is possibility of memory overlap, how to prevent it?

You can make your algorithm work with or without an overlap by deciding to copy overlapping regions from the back if src has numerically lower address than dest.

Note: Since you are doing memcpy, not strcpy, forcing null termination with newdest[i]='\0' is incorrect, and needs to be removed.


问:
PHP http_build_query with numbers in string
I am trying to build a query string with http_build_query, and it is producing unexpected results when a number is passed as one of the values in the array.

```<code>$number = '123';
$data   = array(
    'first'     => $number,
    'second'    => 'test',
);
http_build_query( $data );
//output is first%123&second=test
//unexpected output is % after first instead of =

$number = '500';
$data   = array(
    'first'     => $number,
    'second'    => 'test',
);
http_build_query( $data );
//output is firstP0&second=test
//unexpected output is P0 after first instead of =500

$number = '1';
$data   = array(
    'first'     => $number,
    'second'    => 'test',
);
http_build_query( $data );
//output is first=1&second=test
//output has an equal sign and not a percent sign
</code>```

In the first example, there is a percent sign in the place where I would expect there to be an equal sign.

The second example changes =500 to P0.

The third example is what I'd expect all the time, but for some reason, it seems like the length of the number is changing the output between a % and =

Note: I tried this with various length numbers (1, 12, 1234, 12345, etc.), and with different numbers. Only the single digit example (regardless of whether I use 1, 2, 3, 4, etc.) is able to produce the equal sign like expected.

Is there a way to pass numbers to http_build_query() where it will not have an unexpected output like I referenced in the examples?

答:
It seems like this was a quirk with either the PHP version the site was running on or something in the server settings. I tried the code on several other servers with different PHP versions and it worked correctly.

The server setup I was using where the unexpected output occurred was using PHP v5.5.24 and Apache v2.4.12. The localhost server was run on DesktopServer version 3.8.3.

I haven't been able to identify the specific issue that caused this to happen, but when I ran the same code on a different server with PHP 7 it worked as expected. I also tried various other PHP versions on different servers and all worked correctly.

One of the following is probably the issue:

(Most likely) There is something set in php.ini or some other server setting that is causing the site to execute the function incorrectly. I, unfortunately, haven't figured out exactly what the issue is yet though (will update this answer if I find out).

(Less likely) PHP 5.5.24 has an issue that doesn't output that function correctly. Although there hasn't been a change to http_build_query since PHP 5.4.0 so if there was an issue it should still be present in PHP 7.

Body:
I am trying to build a query string with http_build_query, and it is producing unexpected results when a number is passed as one of the values in the array.

$number = '123';
$data   = array(
    'first'     => $number,
    'second'    => 'test',
);
http_build_query( $data );
//output is first%123&second=test
//unexpected output is % after first instead of =

$number = '500';
$data   = array(
    'first'     => $number,
    'second'    => 'test',
);
http_build_query( $data );
//output is firstP0&second=test
//unexpected output is P0 after first instead of =500

$number = '1';
$data   = array(
    'first'     => $number,
    'second'    => 'test',
);
http_build_query( $data );
//output is first=1&second=test
//output has an equal sign and not a percent sign

In the first example, there is a percent sign in the place where I would expect there to be an equal sign.

The second example changes =500 to P0.

The third example is what I'd expect all the time, but for some reason, it seems like the length of the number is changing the output between a % and =

Note: I tried this with various length numbers (1, 12, 1234, 12345, etc.), and with different numbers. Only the single digit example (regardless of whether I use 1, 2, 3, 4, etc.) is able to produce the equal sign like expected.

Is there a way to pass numbers to http_build_query() where it will not have an unexpected output like I referenced in the examples?


问:
asp .net mvc: Populating select option with ajax
I'm populating a select option using jquery ajax with json data and its working fine, but when I click submit button,  my option value become null. Here's my code:

View:

```<code>@using (Html.BeginForm("Index", "Teste", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="row">
    <div class="col-md-8">
        Data Agendamento
    </div>
    <div class="col-md-8">
        @Html.TextBox("DataAgendamento", null, new { @onchange = "horarios();" })
    </div>
    <div class="col-md-8">
        Hora Agendamento
    </div>
    <div class="col-md-8">
        <select id="HoraAgendamento" name="HoraAgendamento"></select>
    </div>
    <input type="submit" value="Enviar" class="btn btn-default" />
</div>
}
<script>
    function horarios() {
        var Dia = $('#DataAgendamento').val();
        $.ajax({
            url: '/Teste/Popula',
            type: "GET",
            dataType: "JSON",
            data: { dia: Dia },
            success: function (horarios) {
                $("#HoraAgendamento").html("");
                $.each(horarios, function (i, hora) {
                    console.log(i);
                    console.log(hora.Hora);
                    $("#HoraAgendamento").append(
                        $('<option></option>').val(hora.Id).text(hora.Hora));
                });
            }
        });
    }
</script>
</code>```

My Controller(until the problem happened)

```<code>[HttpPost]
    public ActionResult Index(FormCollection form)
    {

        string dt_data;
        dt_data = form["DataAgendamento"] + " " + form["HoraAgendamento"];
</code>```

The problem is I'm getting form["HoraAgendamento"] as null.

答:
Here's an odd solution but it's working now.

I have just replaced hora.Id by hora.Hora in my JavaScript as bellow

Old one:

```<code>$('<option></option>').val(hora.Id).text(hora.Hora));
</code>```

New one:

```<code>$('<option></option>').val(hora.Hora).text(hora.Hora));
</code>```

Thank you all.

Body:
I'm populating a select option using jquery ajax with json data and its working fine, but when I click submit button,  my option value become null. Here's my code:

View:

@using (Html.BeginForm("Index", "Teste", FormMethod.Post))
{
@Html.AntiForgeryToken()

        Data Agendamento

        @Html.TextBox("DataAgendamento", null, new { @onchange = "horarios();" })

        Hora Agendamento

        <select id="HoraAgendamento" name="HoraAgendamento"></select>

    <input type="submit" value="Enviar" class="btn btn-default" />

}

My Controller(until the problem happened)

[HttpPost]
    public ActionResult Index(FormCollection form)
    {

        string dt_data;
        dt_data = form["DataAgendamento"] + " " + form["HoraAgendamento"];

The problem is I'm getting form["HoraAgendamento"] as null.

Body:
Here's an odd solution but it's working now.

I have just replaced hora.Id by hora.Hora in my JavaScript as bellow

Old one:

$('<option></option>').val(hora.Id).text(hora.Hora));

New one:

$('<option></option>').val(hora.Hora).text(hora.Hora));

Thank you all.


问:
How to make module (such as bcrypt) available globally in Express?
I have several "routes" files where I import modules like this:

```<code>var bcrypt = require('bcryptjs');
</code>```

I have attempted to make bcrypt available globally by importing it into my main app js and then using an app.use() something like this:

```<code>var bcrypt = require('bcryptjs');
app.use(bcrypt); // clearly not right, app crashes
</code>```

I've tried a variety of things here. Should I just stick with importing this into every file separately or is there a good way to make this module available globally?

答:
Though you've already accepted an answer, I thought I'd describe a lot more about why you don't want to use the global and why it's desirable to just require() in every module that you need.

The concept behind modules is that you should strive to make each module as independently usable as possible.  In that spirit, there is no reason at all that you should make a module dependent upon a previous module initializing some global variable.  That just makes one module depend upon the exact order of loading with some other module, ruins the ability to ever use that module independently and sets up a potential global variable naming conflict.

A big design goal of the module design in node.js is to avoid using globals.  Instead, a module should either just require() in what it needs or it can be passed required data from other modules when it is loaded or it can query other modules for shared data by calling methods in those other modules.

In addition, loading a module with require() is cached.  So, only the very first time a module is loaded with require() does it load from disk and execute.  Every subsequent time it is loaded, the previously loaded module object is just immediately returned.  Thus, it costs almost nothing to load a module more than once from various modules that need access to it.

So, every module that needs access to bcrypt should just do this:

```<code>const bcrypt = require('bcrytpjs');
</code>```

Yes, this leads to repeating a few more things at the top of each module and that does sometimes feel a little less DRY than you might strive for generally, but it is overall a better tradeoff because it keeps modules as independent as possible as each module independently specifies what other modules it depends on and uses no globals to do that.

Body:
I have several "routes" files where I import modules like this:

var bcrypt = require('bcryptjs');

I have attempted to make bcrypt available globally by importing it into my main app js and then using an app.use() something like this:

var bcrypt = require('bcryptjs');
app.use(bcrypt); // clearly not right, app crashes

I've tried a variety of things here. Should I just stick with importing this into every file separately or is there a good way to make this module available globally?

Body:
Though you've already accepted an answer, I thought I'd describe a lot more about why you don't want to use the global and why it's desirable to just require() in every module that you need.

The concept behind modules is that you should strive to make each module as independently usable as possible.  In that spirit, there is no reason at all that you should make a module dependent upon a previous module initializing some global variable.  That just makes one module depend upon the exact order of loading with some other module, ruins the ability to ever use that module independently and sets up a potential global variable naming conflict.

A big design goal of the module design in node.js is to avoid using globals.  Instead, a module should either just require() in what it needs or it can be passed required data from other modules when it is loaded or it can query other modules for shared data by calling methods in those other modules.

In addition, loading a module with require() is cached.  So, only the very first time a module is loaded with require() does it load from disk and execute.  Every subsequent time it is loaded, the previously loaded module object is just immediately returned.  Thus, it costs almost nothing to load a module more than once from various modules that need access to it.

So, every module that needs access to bcrypt should just do this:

const bcrypt = require('bcrytpjs');

Yes, this leads to repeating a few more things at the top of each module and that does sometimes feel a little less DRY than you might strive for generally, but it is overall a better tradeoff because it keeps modules as independent as possible as each module independently specifies what other modules it depends on and uses no globals to do that.


问:
An elasticsearch query on an optionally absent field
I have an elastic index of simple objects that have two fields:

```<code>{
   "content": "something",
   "acl": ["andrey", "gilles"]
}
</code>```

The acl field is an array of usernames, or a value "public", or it may not be present in an object at all. I want to write a query that gives me all objects where acl=testuser or it doesn't exist. This is my query:

```<code>{
  "query": {
    "bool": {
      "should": [
        {
          "bool" : {
            "must": [
              {"exists" : { "field" : "acl" }},
              {"match" : {"acl":"testuser"}}
            ]
          },
          "bool": {
            "must_not": [
                "exists": {
                    "field": "acl"
                }
            ]
          }
        }
      ]
    }
  }
}
</code>```

But whenI execute it I get an error:

```<code>{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 12,
        "col": 11
      }
    ],
    "type": "parsing_exception",
    "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 12,
    "col": 11
  },
  "status": 400
}
</code>```

Any ideas what I'm doing wrong?

答:
Your JSON is invalid:

You have a field bool set twice inside should query

must_not is an array, and you didn't add curly braces for a new object

Besides, you don't need exist query to match together with match query. So you just need to leave match query and must_not exist query inside should like this:

```<code>{
    "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "must_not": [
                            {
                                "exists": {
                                    "field": "acl"
                                }
                            }
                        ]
                    }
                },
                {
                    "match": {
                        "acl": "testuser"
                    }
                }
            ]
        }
    }
}
</code>```

Body:
I have an elastic index of simple objects that have two fields:

{
   "content": "something",
   "acl": ["andrey", "gilles"]
}

The acl field is an array of usernames, or a value "public", or it may not be present in an object at all. I want to write a query that gives me all objects where acl=testuser or it doesn't exist. This is my query:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool" : {
            "must": [
              {"exists" : { "field" : "acl" }},
              {"match" : {"acl":"testuser"}}
            ]
          },
          "bool": {
            "must_not": [
                "exists": {
                    "field": "acl"
                }
            ]
          }
        }
      ]
    }
  }
}

But whenI execute it I get an error:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 12,
        "col": 11
      }
    ],
    "type": "parsing_exception",
    "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 12,
    "col": 11
  },
  "status": 400
}

Any ideas what I'm doing wrong?

Body:
Your JSON is invalid:

You have a field bool set twice inside should query

must_not is an array, and you didn't add curly braces for a new object

Besides, you don't need exist query to match together with match query. So you just need to leave match query and must_not exist query inside should like this:

{
    "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "must_not": [
                            {
                                "exists": {
                                    "field": "acl"
                                }
                            }
                        ]
                    }
                },
                {
                    "match": {
                        "acl": "testuser"
                    }
                }
            ]
        }
    }
}


问:
Mode parameter in chmod?
I'm trying to figure out what the mode parameter of setfmode() which is called eventually by chmod() is.

When I print it out and do something like chmod +t test.txt "33700" gets printed out. When I do chmod +w test.txt "33252" gets printed out.

Is there a way to see which specific bits are being set using these numbers?

答:
Refer to the FreeBSD manual page at https://www.freebsd.org/doc/handbook/permissions.html for a full description of the permission bits.  Note that the illustrations of permission bits are in octal format.

Search engines are your friends - I searched for "FreeBSD permission bits" and instantly found the above link as the first returned result.

From chat:

"I'm trying to find out if the user is setting the sticky bit, so in setfmode() I think I'll bitwise AND the mode variable with the sticky bit constant, then check if that equals the sticky bit constant"

The sticky bit is octal 1000 - see the FreeBSD chmod man page
To write numbers in octal in C, precede the value with a 0, so assuming your mode value is in a variable named mode, do (mode & 01000). If that value is zero, the sticky bit is not set, if it is non-zero (i.e., 01000), the sticky bit is set.

Mark: "Ok my check seems to be working -
if ((mode & S_ISTXT) == S_ISTXT)"

Title: Mode parameter in chmod?