Panic in Golang http.Client with high concurrent excecutions - multithreading

I am creating a system which is a http server in golang that will perform several request to another API based in every request that come to it.
e.g
curl localhost:8080/users?ids=1,2,3,4
will perform several concurrent gets to:
api.com/user/1
api.com/user/2
api.com/user/3
api.com/user/4
I am having a problem, the http.Client is getting my a panic, when it has a heavy concurrent requests (if I hit localhost:8080/users?ids=1,2,3,4.....40 with AB with 4 concurrent, or hitting refresh in my browser)
The proglem appears to be with the sentence (line 159)
resp, _ := client.Do(req)
My code is here (Not so large... 180 lines):
http://play.golang.org/p/olibNz2n1Z
The panic error is this one:
goroutine 5 [select]:
net/http.(*persistConn).roundTrip(0xc210058f80, 0xc21000a720, 0xc210058f80, 0x0, 0x0)
/usr/local/go/src/pkg/net/http/transport.go:879 +0x6d6
net/http.(*Transport).RoundTrip(0xc210058280, 0xc21005b1a0, 0x1, 0x0, 0x0)
/usr/local/go/src/pkg/net/http/transport.go:187 +0x391
net/http.send(0xc21005b1a0, 0x590290, 0xc210058280, 0x0, 0x0, ...)
/usr/local/go/src/pkg/net/http/client.go:168 +0x37f
net/http.(*Client).send(0xc21001e960, 0xc21005b1a0, 0x28, 0xc21001ec30, 0xc21005f570)
/usr/local/go/src/pkg/net/http/client.go:100 +0xd9
net/http.(*Client).doFollowingRedirects(0xc21001e960, 0xc21005b1a0, 0x2ab298, 0x0, 0x0, ...)
/usr/local/go/src/pkg/net/http/client.go:294 +0x671
net/http.(*Client).Do(0xc21001e960, 0xc21005b1a0, 0xa, 0x0, 0x0)
/usr/local/go/src/pkg/net/http/client.go:129 +0x8f
main.buscarRecurso(0xc21000a650, 0xb, 0xc2100526c0)
/Users/fscasserra/Documents/workspace/Luna/multiget-api/multiget.go:159 +0x131
created by main.obtenerRecursos
/Users/fscasserra/Documents/workspace/Luna/multiget-api/multiget.go:106 +0x197
Can anyone help me?
Best regards,
Fer

I will put money on the panic coming from calling Close() on a nil resp.Body.
Always check your errors!
In general, if a function returns a value and an error, the response value may not be usable in the case of a non-nil error. Any exceptions to this should be well documented.

Related

XMonad not giving space for Tint2 bar

I am currently setting up my new config for XMonad but I cannot get the docks to work.
https://hastebin.com/eqoliyodat.swift
The tint properties look like this.
WM_STATE(WM_STATE):
window state: Normal
icon window: 0x0
WM_NORMAL_HINTS(WM_SIZE_HINTS):
program specified location: 1559250944, -874843230
program specified minimum size: 127 by 42
program specified maximum size: 127 by 42
WM_CLASS(STRING) = "tint2", "Tint2"
XdndAware(ATOM) = ATOM
_MOTIF_WM_HINTS(_MOTIF_WM_HINTS) = 0x2, 0x0, 0x0, 0x0, 0x0
WM_HINTS(WM_HINTS):
Client accepts input or input focus: False
Initial state is Don't Care State.
window id # to use for icon: 0xe00003
window id # of group leader: 0xe00003
_NET_WM_STATE(ATOM) = _NET_WM_STATE_SKIP_PAGER, _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_STICKY, _NET_WM_STATE_ABOVE
_NET_WM_DESKTOP(CARDINAL) = 4294967295
_NET_WM_WINDOW_TYPE(ATOM) = _NET_WM_WINDOW_TYPE_DOCK
_NET_WM_PID(CARDINAL) = 11648
_NET_WM_ICON_NAME(UTF8_STRING) = "tint2"
_NET_WM_NAME(UTF8_STRING) = "tint2"
WM_ICON_NAME(STRING) = "tint2"
WM_NAME(STRING) = "tint2"
I have no idea where this could be coming from, can somebody help me?
I was looking through the current threads, but didnt seem to find any other solution to my problem. I cant use the normal gaps, because I have multiple monitors but a bar only on one screen.
Best regards.

Error when reading/writing from CAN and sensor simultaneously over I2C

I am using a can board and an IMU MPU6050 to be able use motors while also reading an angle from the IMU.
When using them separately it works, the IMU reads and the CAN can activate and run the motors. The issue arises when both are activated at the same time and the script gives the following error:
IOError: [Errno 5] Input/output error
Here are the registers:
# MPU6050 Registers
MPU6050_ADDR = 0x68#0x0c
PWR_MGMT_1 = 0x6B
SMPLRT_DIV = 0x19
CONFIG = 0x1A
GYRO_CONFIG = 0x1B
ACCEL_CONFIG = 0x1C
INT_PIN_CFG = 0x37
INT_ENABLE = 0x38
ACCEL_XOUT_H = 0x3B
ACCEL_YOUT_H = 0x3D
And fault messages arises when I try to write a byte to the IMU at the same time as the CAN bus is activated:
# Write to Configuration register
bus.write_byte_data(MPU6050_ADDR, CONFIG, 0)
I created a new I2C bus for the IMU which is bus #3 which gives me the addresses 0x68 and 0x0c.
I have checked the wiring which is correct. Otherwise I should get the same error when running them separately.
Does anyone have any suggestions on what to try next?

Azure CLI VHD upload failing at 0.5 %

so, I have been trying for hours now to upload a VHD to Azure.
I downloaded a VHD by exporting on one Azure tenant on another domain, now I'm trying to upload it on another Azure account to attach it to a VM.
Steps (based on this article):
Exported VHD on tenant 1 (T1)
Logged into Azure CLI on PC with tenant 2 (T2)
Created a disk through Azure CLI T2 with upload bytes parameter set (30GB - 32213303808 bytes)
Granted access to disk on T2
Started upload with AzCopy.exe copy "D:\Downloads\disk.vhd "https://[sasurl]" --blob-type PageBlob
As soon as the upload starts, it gets stuck on 0.5 %, after about 55 minutes it just spits out that the upload has failed.
Log file:
2021/02/12 20:08:27 0.5 %, 0 Done, 0 Failed, 1 Pending, 0 Skipped, 1 Total, 2-sec Throughput (Mb/s): 14.1551
2021/02/12 20:08:28 INFO: [P#0-T#0] Page blob throughput tuner: Target Mbps 4048
2021/02/12 20:08:29 INFO: [P#0-T#0] Page blob throughput tuner: Target Mbps 4054
2021/02/12 20:08:29 PERF: primary performance constraint is Unknown. States: X: 0, O: 0, M: 0, L: 0, R: 1, D: 0, W: 480, F: 0, B: 96, E: 0, T: 577, GRs: 96
2021/02/12 20:08:29 0.5 %, 0 Done, 0 Failed, 1 Pending, 0 Skipped, 1 Total, 2-sec Throughput (Mb/s): 11.1397
2021/02/12 20:08:30 INFO: [P#0-T#0] Page blob throughput tuner: Target Mbps 4060
2021/02/12 20:08:31 INFO: [P#0-T#0] Page blob throughput tuner: Target Mbps 4066
2021/02/12 20:08:31 ==> REQUEST/RESPONSE (Try=1/11.1957784s[SLOW >3s], OpTime=11.1957784s) -- REQUEST ERROR
PUT [redacted SAS url]
Content-Length: [4194304]
User-Agent: [AzCopy/10.8.0 Azure-Storage/0.10 (go1.13; Windows_NT)]
X-Ms-Client-Request-Id: [redacted]
X-Ms-Page-Write: [update]
X-Ms-Range: [bytes=398458880-402653183]
X-Ms-Version: [2019-12-12]
--------------------------------------------------------------------------------
ERROR:
-> github.com/Azure/azure-pipeline-go/pipeline.NewError, /home/vsts/go/pkg/mod/github.com/!azure/azure-pipeline-go#v0.2.3/pipeline/error.go:157
HTTP request failed
Put [redacted SAS url]: net/http: TLS handshake timeout
goroutine 214 [running]:
github.com/Azure/azure-storage-azcopy/ste.stack(0xc092e4ea20, 0xc04e193300, 0x0)
/home/vsts/work/1/s/ste/xferLogPolicy.go:232 +0xa4
github.com/Azure/azure-storage-azcopy/ste.NewRequestLogPolicyFactory.func1.1(0xdb74a0, 0xc000653560, 0xc001189300, 0x10, 0x3, 0x0, 0xc00168fa20)
/home/vsts/work/1/s/ste/xferLogPolicy.go:146 +0x7ac
github.com/Azure/azure-pipeline-go/pipeline.PolicyFunc.Do(0xc00175c640, 0xdb74a0, 0xc000653560, 0xc001189300, 0xc00168faf0, 0x2030004, 0xa, 0x2030005)
/home/vsts/go/pkg/mod/github.com/!azure/azure-pipeline-go#v0.2.3/pipeline/core.go:43 +0x4b
github.com/Azure/azure-storage-azcopy/ste.NewVersionPolicyFactory.func1.1(0xdb74a0, 0xc000653560, 0xc001189300, 0xc0010cac40, 0xc0010cabf0, 0x40c7c0, 0xc00175c6e0)
/home/vsts/work/1/s/ste/mgr-JobPartMgr.go:78 +0x1b8
github.com/Azure/azure-pipeline-go/pipeline.PolicyFunc.Do(0xc00126c2e0, 0xdb74a0, 0xc000653560, 0xc001189300, 0x10, 0x10, 0xb939e0, 0xa)
/home/vsts/go/pkg/mod/github.com/!azure/azure-pipeline-go#v0.2.3/pipeline/core.go:43 +0x4b
github.com/Azure/azure-storage-blob-go/azblob.responderPolicy.Do(0xda72e0, 0xc00126c2e0, 0xc0011b0820, 0xdb74a0, 0xc000653560, 0xc001189300, 0x0, 0x12e2360, 0xc0010cac50, 0x4310d8)
/home/vsts/go/pkg/mod/github.com/!azure/azure-storage-blob-go#v0.10.1-0.20201022074806-8d8fc11be726/azblob/zz_generated_responder_policy.go:33 +0x5a
github.com/Azure/azure-storage-blob-go/azblob.anonymousCredentialPolicy.Do(...)
/home/vsts/go/pkg/mod/github.com/!azure/azure-storage-blob-go#v0.10.1-0.20201022074806-8d8fc11be726/azblob/zc_credential_anonymous.go:54
github.com/Azure/azure-storage-azcopy/ste.(*retryNotificationPolicy).Do(0xc001234560, 0xdb74a0, 0xc000653560, 0xc001189300, 0x0, 0x0, 0x0, 0xc0010cad70)
/home/vsts/work/1/s/ste/xferRetryNotificationPolicy.go:59 +0x62
github.com/Azure/azure-pipeline-go/pipeline.PolicyFunc.Do(0xc0012345c0, 0xdb74a0, 0xc000653560, 0xc001189300, 0xc000653560, 0xc001234dd0, 0xc000000001, 0x0)
/home/vsts/go/pkg/mod/github.com/!azure/azure-pipeline-go#v0.2.3/pipeline/core.go:43 +0x4b
github.com/Azure/azure-storage-azcopy/ste.NewBlobXferRetryPolicyFactory.func1.1(0xdb74e0, 0xc0001c7440, 0xc001189200, 0x10, 0xb411a0, 0x64492d747301, 0xc00168f340)
/home/vsts/work/1/s/ste/xferRetrypolicy.go:384 +0x70f
github.com/Azure/azure-pipeline-go/pipeline.PolicyFunc.Do(0xc00175c690, 0xdb74e0, 0xc0001c7440, 0xc001189200, 0xc00168f440, 0x30, 0x28, 0x8)
/home/vsts/go/pkg/mod/github.com/!azure/azure-pipeline-go#v0.2.3/pipeline/core.go:43 +0x4b
github.com/Azure/azure-storage-blob-go/azblob.NewUniqueRequestIDPolicyFactory.func1.1(0xdb74e0, 0xc0001c7440, 0xc001189200, 0x10, 0xb411a0, 0x40d001, 0xc00168f340)
/home/vsts/go/pkg/mod/github.com/!azure/azure-storage-blob-go#v0.10.1-0.20201022074806-8d8fc11be726/azblob/zc_policy_unique_request_id.go:19 +0xb5
github.com/Azure/azure-pipeline-go/pipeline.PolicyFunc.Do(0xc00126c320, 0xdb74e0, 0xc0001c7440, 0xc001189200, 0xc00168f428, 0x35, 0xc0007d03c0, 0xc0010cb138)
/home/vsts/go/pkg/mod/github.com/!azure/azure-pipeline-go#v0.2.3/pipeline/core.go:43 +0x4b
github.com/Azure/azure-storage-blob-go/azblob.NewTelemetryPolicyFactory.func1.1(0xdb74e0, 0xc0001c7440, 0xc001189200, 0x1, 0x0, 0x1, 0xc0017100a0)
/home/vsts/go/pkg/mod/github.com/!azure/azure-storage-blob-go#v0.10.1-0.20201022074806-8d8fc11be726/azblob/zc_policy_telemetry.go:34 +0x164
github.com/Azure/azure-pipeline-go/pipeline.PolicyFunc.Do(0xc0001c74d0, 0xdb74e0, 0xc0001c7440, 0xc001189200, 0xc0001c74d0, 0xc001189200, 0xc0010cb208, 0x40d06f)
/home/vsts/go/pkg/mod/github.com/!azure/azure-pipeline-go#v0.2.3/pipeline/core.go:43 +0x4b
github.com/Azure/azure-pipeline-go/pipeline.(*pipeline).Do(0xc0007ce040, 0xdb74e0, 0xc0001c7440, 0xda73e0, 0xc0011b0820, 0xc001189200, 0x30, 0xc000000638, 0x12, 0x0)
/home/vsts/go/pkg/mod/github.com/!azure/azure-pipeline-go#v0.2.3/pipeline/core.go:129 +0x88
github.com/Azure/azure-storage-blob-go/azblob.pageBlobClient.UploadPages(0xc000000600, 0x5, 0x0, 0x0, 0x0, 0xc000000608, 0x30, 0xc000000638, 0x12, 0x0, ...)
/home/vsts/go/pkg/mod/github.com/!azure/azure-storage-blob-go#v0.10.1-0.20201022074806-8d8fc11be726/azblob/zz_generated_page_blob.go:805 +0x5b0
github.com/Azure/azure-storage-blob-go/azblob.PageBlobURL.UploadPages(0xc000000600, 0x5, 0x0, 0x0, 0x0, 0xc000000608, 0x30, 0xc000000638, 0x12, 0x0, ...)
/home/vsts/go/pkg/mod/github.com/!azure/azure-storage-blob-go#v0.10.1-0.20201022074806-8d8fc11be726/azblob/url_page_blob.go:87 +0x336
github.com/Azure/azure-storage-azcopy/ste.(*pageBlobUploader).GenerateUploadFunc.func1()
/home/vsts/work/1/s/ste/sender-pageBlobFromLocal.go:96 +0x8f3
github.com/Azure/azure-storage-azcopy/ste.createChunkFunc.func1(0x3c)
/home/vsts/work/1/s/ste/sender.go:179 +0x1ae
github.com/Azure/azure-storage-azcopy/ste.(*jobsAdmin).chunkProcessor(0xc000054000, 0x3c)
/home/vsts/work/1/s/ste/JobsAdmin.go:433 +0xe6
created by github.com/Azure/azure-storage-azcopy/ste.(*jobsAdmin).poolSizer
/home/vsts/work/1/s/ste/JobsAdmin.go:362 +0x682
2021/02/12 20:08:31 ==> REQUEST/RESPONSE (Try=1/11.1957784s[SLOW >3s], OpTime=11.1957784s) -- REQUEST ERROR
PUT [redacted SAS url]
Content-Length: [4194304]
User-Agent: [AzCopy/10.8.0 Azure-Storage/0.10 (go1.13; Windows_NT)]
X-Ms-Client-Request-Id: [redacted]
X-Ms-Page-Write: [update]
X-Ms-Range: [bytes=394264576-398458879]
X-Ms-Version: [2019-12-12]
--------------------------------------------------------------------------------
ERROR:
-> github.com/Azure/azure-pipeline-go/pipeline.NewError, /home/vsts/go/pkg/mod/github.com/!azure/azure-pipeline-go#v0.2.3/pipeline/error.go:157
HTTP request failed
And it just goes on like this for thousands of lines, at the end of the log there's this:
2021/02/12 19:56:56 INFO: [P#0-T#0] Page blob throughput tuner: Target Mbps 98705
2021/02/12 19:56:57 ==> REQUEST/RESPONSE (Try=14/2m0.3843703s[SLOW >3s], OpTime=38m15.4275233s) -- RESPONSE STATUS CODE ERROR
PUT https://md-impexp-vpwbn0cr4mj2.z18.blob.storage.azure.net/3ntgq1cq2rsj/abcd?comp=page&si=a01312d5-4da8-41d1-acc6-7466f702a447&sig=-REDACTED-&sr=b&sv=2018-03-28&timeout=901
Content-Length: [4194304]
User-Agent: [AzCopy/10.8.0 Azure-Storage/0.10 (go1.13; Windows_NT)]
X-Ms-Client-Request-Id: [36e705e8-9f5d-45a1-4574-7e994292da82]
X-Ms-Page-Write: [update]
X-Ms-Range: [bytes=398458880-402653183]
X-Ms-Version: [2019-12-12]
--------------------------------------------------------------------------------
RESPONSE Status: 500 Operation could not be completed within the specified time.
Content-Length: [246]
Content-Type: [application/xml]
Date: [Fri, 12 Feb 2021 19:56:56 GMT]
Server: [Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0]
X-Ms-Client-Request-Id: [36e705e8-9f5d-45a1-4574-7e994292da82]
X-Ms-Error-Code: [OperationTimedOut]
X-Ms-Request-Id: [f91ed41a-c01e-0008-4f78-011552000000]
X-Ms-Version: [2019-12-12]
Response Details: <?xml version="1.0" encoding="utf-8"?> <Error><Code>OperationTimedOut</Code><Message>Operation could not be completed within the specified time. </Message>
So what am I doing wrong? Tried both CLI and PowerShell.
I stumbled upon a good solution myself:
Since I was uploading a VHD of a disk already on Azure, instead of using the downloaded VHD I used the original VHD export URL as the source of the disk.

golang spanner test library crashes after a while

Spanner GO library crashes after few mins perhaps after this query (although this has been successful earlier)
Version cloud.google.com/go/spanner v1.11.0
2021/02/01 00:45:32.564971 spannertest.inmem: Querying: SELECT * FROM tenant_config WHERE commit_time > "2021-02-01T00:44:32Z"
Crashinfo
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0xae3bcb]
goroutine 214 [running]:
cloud.google.com/go/spanner/spannertest.(*server).ExecuteSql(0xc00009b4a0, 0xf74c00, 0xc0001e8270, 0xc0003e8c60, 0xc00009b4a0, 0xc0001e8270, 0xc0008a4ba0)
/Users/mpathak/Development/gopkgs/pkg/mod/cloud.google.com/go/spanner#v1.11.0/spannertest/inmem.go:491 +0x3b
google.golang.org/genproto/googleapis/spanner/v1._Spanner_ExecuteSql_Handler(0xd0f8a0, 0xc00009b4a0, 0xf74c00, 0xc0001e8270, 0xc00088b020, 0x0, 0xf74c00, 0xc0001e8270, 0xc0004f4060, 0x14)
/Users/mpathak/Development/gopkgs/pkg/mod/google.golang.org/genproto#v0.0.0-20201019141844-1ed22bb0c154/googleapis/spanner/v1/spanner.pb.go:3581 +0x217
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0003a1500, 0xf7e800, 0xc00018a900, 0xc00089a000, 0xc0001ac2a0, 0x152c7d8, 0x0, 0x0, 0x0)
/Users/mpathak/Development/gopkgs/pkg/mod/google.golang.org/grpc#v1.32.0/server.go:1194 +0x50a
google.golang.org/grpc.(*Server).handleStream(0xc0003a1500, 0xf7e800, 0xc00018a900, 0xc00089a000, 0x0)
/Users/mpathak/Development/gopkgs/pkg/mod/google.golang.org/grpc#v1.32.0/server.go:1517 +0xcfd
google.golang.org/grpc.(*Server).serveStreams.func1.2(0xc000606140, 0xc0003a1500, 0xf7e800, 0xc00018a900, 0xc00089a000)
/Users/mpathak/Development/gopkgs/pkg/mod/google.golang.org/grpc#v1.32.0/server.go:859 +0xa1
created by google.golang.org/grpc.(*Server).serveStreams.func1
/Users/mpathak/Development/gopkgs/pkg/mod/google.golang.org/grpc#v1.32.0/server.go:857 +0x204
This seems to be caused by a bug in the ExecuteSql method implementation of spannertest. The session pool of the Spanner client will execute a ping statement every 50 minutes to keep sessions alive on the backend. These SELECT 1 statements are executed without a transaction, which means that the backend should default to a single-use read-only transaction. The inmem server of spannertest assumes that the client will always specify a TransactionSelector: https://github.com/googleapis/google-cloud-go/blob/c7ecf0f3f454606b124e52d20af2545b2c68646f/spanner/spannertest/inmem.go#L491
I've opened an issue for it here: https://github.com/googleapis/google-cloud-go/issues/3639

Get number of opened application windows in linux

I want to detect how many instances of a specific application, managed by the window manager, are opened. At the moment, I have this:
#!/bin/bash
# wmctrl required
pids=$(pidof $1)
IFS=' ' read -a pid_arr <<< "$pids"
matches=0
for pid in "${pid_arr[#]}"
do
matching_lines=$(wmctrl -l -p | egrep -c "^.+\b.+\b$pid\b")
matches=$((matches + $matching_lines))
done
echo $matches
Say I have firefox open three times and I pass firefox as an argument, the function will only return the right number if there were no "sub-windows" (e.g. preference windows) opened. Otherwise, the function will count these windows too.
I want to get the number of windows without the "sub-windows".
P.S.: Made some changes. To count the number of windows without their dialog-windows, this is my current solution:
#!/bin/bash
pids=$(pidof $1)
IFS=' ' read -r -a pid_arr <<< "$pids"
matches=0
for pid in "${pid_arr[#]}"
do
ids=$(wmctrl -l -p | awk '$3=='$pid'{printf $1" "}')
IFS=' ' read -r -a id_arr <<< "$ids"
for id in "${id_arr[#]}"
do
if ! xprop -id "$id" |
egrep -q '(WM_TRANSIENT_FOR|_NET_WM_WINDOW_TYPE_DIALOG)'
then
((matches++))
fi
done
done
echo $matches
But in the case of firefox, the script cannot distinguish between the browser window and its preference window because none of them is a dialog window.
The above script is sufficient for me, because I use it to figure out how many windows (without dialog windows) of xfce4-appfinder are open. For xfce4-appfinder the script works, because its preference window can easily be recognized as a dialog window. In firefox the preference window can even be opened standalone by calling firefox -preferences. To recognize windows like this as some kind of "sub-window" is maybe impossible.
Here are my results of xprop and xwininfo for both the browser and preference window of firefox:
xwininfo (browser):
xwininfo: Window id: 0x1c0007f "bash - Get number of opened application windows in linux - Stack Overflow - Mozilla Firefox"
Root window id: 0xa1 (the root window) (has no name)
Parent window id: 0x1400048 (has no name)
1 child:
0x1c00080 (has no name): () 1x1+-1+-1 +1+37
Absolute upper-left X: 2
Absolute upper-left Y: 38
Relative upper-left X: 0
Relative upper-left Y: 14
Width: 956
Height: 511
Depth: 24
Visual: 0x20
Visual Class: TrueColor
Border width: 0
Class: InputOutput
Colormap: 0x22 (installed)
Bit Gravity State: NorthWestGravity
Window Gravity State: NorthWestGravity
Backing Store State: NotUseful
Save Under State: no
Map State: IsViewable
Override Redirect State: no
Corners: +2+38 -962+38 -962-531 +2-531
-geometry 956x511+0+22
Bit gravity: NorthWestGravity
Window gravity: NorthWestGravity
Backing-store hint: NotUseful
Backing-planes to be preserved: 0xffffffff
Backing pixel: 0
Save-unders: No
Someone wants these events:
KeyPress
KeyRelease
ButtonPress
ButtonRelease
EnterWindow
LeaveWindow
PointerMotion
Exposure
VisibilityChange
StructureNotify
FocusChange
PropertyChange
Do not propagate these events:
Override redirection?: No
Window manager hints:
Client accepts input or input focus: Yes
Initial state is Normal State
Displayed on desktop 0
Window type:
Normal
Process id: 792 on host T530
Normal window size hints:
Program supplied minimum size: 300 by 71
Program supplied maximum size: 32767 by 32767
Program supplied window gravity: NorthWestGravity
No zoom window size hints defined
No window shape defined
No border shape defined
xwininfo (preferences):
xwininfo: Window id: 0x1c046ef "Firefox Preferences"
Root window id: 0xa1 (the root window) (has no name)
Parent window id: 0x1400060 (has no name)
1 child:
0x1c046f0 (has no name): () 1x1+-1+-1 +961+37
Absolute upper-left X: 962
Absolute upper-left Y: 38
Relative upper-left X: 0
Relative upper-left Y: 14
Width: 956
Height: 1040
Depth: 24
Visual: 0x20
Visual Class: TrueColor
Border width: 0
Class: InputOutput
Colormap: 0x22 (installed)
Bit Gravity State: NorthWestGravity
Window Gravity State: NorthWestGravity
Backing Store State: NotUseful
Save Under State: no0x1c00001
Map State: IsViewable
Override Redirect State: no
Corners: +962+38 -2+38 -2-2 +962-2
-geometry 956x1040-0-0
Bit gravity: NorthWestGravity
Window gravity: NorthWestGravity
Backing-store hint: NotUseful
Backing-planes to be preserved: 0xffffffff
Backing pixel: 0
Save-unders: No
Someone wants these events:
KeyPress
KeyRelease
ButtonPress
ButtonRelease
EnterWindow
LeaveWindow
PointerMotion
Exposure
VisibilityChange
StructureNotify
FocusChange
PropertyChange
Do not propagate these events:
Override redirection?: No
Window manager hints:
Client accepts input or input focus: Yes
Initial state is Normal State
Displayed on desktop 0
Window type:
Normal
Process id: 792 on host T530
Normal window size hints:
Program supplied location: 0, 0
Program supplied minimum size: 604 by 594
Program supplied maximum size: 32767 by 32767
Program supplied window gravity: NorthWestGravity
No zoom window size hints defined
No window shape defined
No border shape defined
xprop (browser):
_DESKTOP(CARDINAL) = 0
WM_STATE(WM_STATE):
window state: Normal
icon window: 0x0
WM_HINTS(WM_HINTS):
Client accepts input or input focus: True
Initial state is Normal State.
bitmap id # to use for icon: 0x1c00082
bitmap id # of mask for icon: 0x1c00083
window id # of group leader: 0x1c00001
_NET_STARTUP_ID(UTF8_STRING) = "xfce4-appfinder/|usr|lib|firefox|firefox/756-0-T530_TIME21351"
WM_WINDOW_ROLE(STRING) = "browser"
XdndAware(ATOM) = BITMAP
_MOTIF_DRAG_RECEIVER_INFO(_MOTIF_DRAG_RECEIVER_INFO) = 0x6c, 0x0, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0
_NET_WM_SYNC_REQUEST_COUNTER(CARDINAL) = 29360257
_NET_WM_WINDOW_TYPE(ATOM) = _NET_WM_WINDOW_TYPE_NORMAL
_NET_WM_USER_TIME(CARDINAL) = 1758669
_NET_WM_USER_TIME_WINDOW(WINDOW): window id # 0x1c00080
WM_CLIENT_LEADER(WINDOW): window id # 0x1c00001
_NET_WM_PID(CARDINAL) = 792
WM_LOCALE_NAME(STRING) = "en_US.UTF-8"
WM_CLIENT_MACHINE(STRING) = "T530"
WM_NORMAL_HINTS(WM_SIZE_HINTS):
program specified minimum size: 300 by 71
program specified maximum size: 32767 by 32767
window gravity: NorthWest
WM_PROTOCOLS(ATOM): protocols WM_DELETE_WINDOW, WM_TAKE_FOCUS, _NET_WM_PING, _NET_WM_SYNC_REQUEST
WM_CLASS(STRING) = "Navigator", "Firefox"
WM_ICON_NAME(STRING) = "bash - Get number of opened application windows in linux - Stack Overflow - Mozilla Firefox"
_NET_WM_ICON_NAME(UTF8_STRING) = "bash - Get number of opened application windows in linux - Stack Overflow - Mozilla Firefox"
WM_NAME(STRING) = "bash - Get number of opened application windows in linux - Stack Overflow - Mozilla Firefox"
_NET_WM_NAME(UTF8_STRING) = "bash - Get number of opened application windows in linux - Stack Overflow - Mozilla Firefox"
xprop (preferences)
_NET_WM_DESKTOP(CARDINAL) = 0
WM_STATE(WM_STATE):
window state: Normal
icon window: 0x0
WM_HINTS(WM_HINTS):
Client accepts input or input focus: True
Initial state is Normal State.
bitmap id # to use for icon: 0x1c046f2
bitmap id # of mask for icon: 0x1c046f3
window id # of group leader: 0x1c00001
WM_WINDOW_ROLE(STRING) = "Preferences"
XdndAware(ATOM) = BITMAP
_MOTIF_DRAG_RECEIVER_INFO(_MOTIF_DRAG_RECEIVER_INFO) = 0x6c, 0x0, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0
_NET_WM_SYNC_REQUEST_COUNTER(CARDINAL) = 29378289
_NET_WM_WINDOW_TYPE(ATOM) = _NET_WM_WINDOW_TYPE_NORMAL
_NET_WM_USER_TIME(CARDINAL) = 1456410
_NET_WM_USER_TIME_WINDOW(WINDOW): window id # 0x1c046f0
WM_CLIENT_LEADER(WINDOW): window id # 0x1c00001
_NET_WM_PID(CARDINAL) = 792
WM_LOCALE_NAME(STRING) = "en_US.UTF-8"
WM_CLIENT_MACHINE(STRING) = "T530"
WM_NORMAL_HINTS(WM_SIZE_HINTS):
program specified location: 0, 0
program specified minimum size: 604 by 594
program specified maximum size: 32767 by 32767
window gravity: NorthWest
WM_PROTOCOLS(ATOM): protocols WM_DELETE_WINDOW, WM_TAKE_FOCUS, _NET_WM_PING, _NET_WM_SYNC_REQUEST
WM_CLASS(STRING) = "Browser", "Firefox"
WM_ICON_NAME(STRING) = "Firefox Preferences"
_NET_WM_ICON_NAME(UTF8_STRING) = "Firefox Preferences"
WM_NAME(STRING) = "Firefox Preferences"
_NET_WM_NAME(UTF8_STRING) = "Firefox Preferences"
I don't know that there's a simple answer to this. You are probably going to need to inspect the windows more closely and weed-out any that aren't what you want.
The group leader property on a window might be useful. (Some sub-windows should have this and main windows may not).
You can also look at the full WM_CLASS and WM_WINDOW_ROLE values which might help you distinguish between window types.
The _NET_WM_WINDOW_TYPE, etc. properties are also likely to be helpful in figuring out what "kind" of window you are looking at.
Play around with xwininfo and xprop on the various windows on your display and you'll see what you can find.
The follow give you a list of running application which have a windows:
(Tested on Debian 10)
# copy to terminal and press enter, for getting list of all programs with window
wmctrl -l
# Or grep if you look only for count of only one application
wmctrl -l -p | grep name_of_application
For getting the count of running programms, count the lines or the programm IDs of the output.

Resources